Solved Insert fill circle into cell of QTableWidget
-
@juaniyoalm
Hi
well the vector <vector <Cell >> matrix could be inside the model
and you would use method like the Addperson in sample to fill it.
Much like you do now. when maxtrix is outside of the model.
You could also fill it in mainwindow and simply give whole matrix to model
to copy it to internal variable.(matrix) but thats a bit waste full.Also i been wanting to ask you. You do use a TableView because you need scrolling correct?
I mean there are far more rows with cells than can fit on on screen ? -
Yes, there is no row limit, it can be 20 rows, 100 or 1000 xD. I always have to think about what I do to behave well when I do the parallel implementation, using pthread and openmp.
-
@juaniyoalm
Ok so scrolling is needed:)
Also make sure to check out QtConcurrent and QThread before jumping to pthread. -
ok, I will look it.
I have a doubt, when I modify a value of the Cell class (this is done in a method of the Cell class ), it will be modified automatically in the model or I have to do model-> setData (model-> index (i, j) , matrix [i] [j] -> getModifiedValue ())? The Cell of matrix are pointer...
-
@juaniyoalm
Well if you go with QAbstractTableModel
your matrix IS the data so if you modify it
and call dataChanged (model call ) to let view know data has been altered. -
ok, this is my model implementation:
/#include "cellmodel.h" CellModel::CellModel(QObject *parent) : QAbstractTableModel(parent){} CellModel::CellModel(int rows, int columns, int fungivores) { this->mundo = new World(rows, columns, fungivores); } int CellModel::rowCount(const QModelIndex &parent) const { return this->mundo->world.size(); } int CellModel::columnCount(const QModelIndex &parent) const { return this->mundo->world[0].size(); } QVariant CellModel::data(const QModelIndex &index, int role) const { if(!index.isValid()) return QVariant(); if(index.row() >= mundo->world.size() || index.row() < 0 || index.column() >= mundo->world[0].size() || index.column() < 0) return QVariant(); if(role == Qt::DisplayRole || role == Qt::EditRole) { return this->mundo->world[index.row()][index.column()]->getFood(); } return QVariant(); } bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole && !(index.row() >= this->mundo->world.size() || index.row() < 0 || index.column() >= this->mundo->world[0].size() || index.column() < 0)) { int row = index.row(); int col = index.column(); this->mundo->world[row][col]->setFood(value.toInt()); emit dataChanged(index, index); return true; } return false; } Qt::ItemFlags CellModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::ItemIsEnabled; return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; }
if I modify the value in this way: model-> world-> world [0] [0] -> setFood (200);
1- All row is changed instead of just the selected cell.
2 - Repaint all matrix, although the data of the others have not changed.
-
hi
If you place break point and check
CellModel::data
return this->mundo->world[index.row()][index.column()]->getFood();
is row and index what you expect and does getFood() return different values?it looks ok.
very much like
http://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-tablemodel-cpp.html -
@mrjj Hi!!
In the variable index I get the whole world object (contains the matrix). Attached image:
I think the problem is in the setData method because if I delete the content of this method (I only leave the return false) the behavior does not change, that is, it continues to paint all row instead of just the one that has been modified.
EDIT:
When I modify the value with the setValue method of the Cell class, it does not pass through the setData method.
-
@juaniyoalm
Hi
is index.row / col always 0,0 ?
you can use QDebug to easy see it
while running. -
No, is 0,0 because I have done it this way to test.
-
@juaniyoalm
ok, so it returns same data from any index() / cell ? -
No, each cell returns a different value, but in the modification of the UI it modifies all row with the value sent.
EDIT:
Is it normal that every time I touch the main window, the debug enters the data method of the model?
-
@juaniyoalm
Ok. Im not sure whats up.
Maybe something with indexes().
Cannot guess it.- the debug enters the data method of the model?
Im not sure what it means ?
- the debug enters the data method of the model?
-
I'm sorry, I wanted to say that, in the execution, when I touch the ui, the execution goes through the Data method and reviews all the indexes.
Was there any way to send you to the app? It's a small app and I need to be able to move forward but I can not find the problem ......
-
@juaniyoalm
Hi
You can link to dropbox or google drive. just zip project folder. -
This is the project:
ProjectA short summary:
At this time only the most basic is implemented.
Cell Class: Contains an array of fungivores and an integer that represents the fungus. When the number of fungivores increases, the circle of the UI should become larger. When the amount of fungus increases or decreases, the color of the cell of the UI should change.
Fungivore Class: At this moment it is only created so that it can be instantiated.
World Class: Contains an array of fungivores (all fungivores of the app that will then be shared between the cells). In addition, it contains a Vector <Vector <Cell >> (Matrix).
-
@juaniyoalm
Hi
some function was missing in NewDialog but i just made them return a valid value.
You model actually seems fine (\o/)
but i found out you would fill the whole
row with same Cell , it seems.
Doing this instead
and little change to Cell ( to to be able to see difference)
Cell::Cell(QObject *parent) : QObject(parent) { this->fungus = 10 + (rand() % 5); }
It now seems to edit fine :)
-
@mrjj Thank you so much!!
But if you modify a cell with the setfood method, does it modify it well?
-
@juaniyoalm
Hi
You mean viavoid MainWindow::changeVal() { model->mundo->world[0][0]->setFood(200); }
Yes it does, but you first see it if u click in cell as you secretly change it directly in list
and the model dont know.so do like this
model->setData(model->index(2,2),200,Qt::EditRole);
and it refesh it self. -
Thanks so much!!
The problem was in World constructor , as you said.
once solved this problem, as you will have noticed, I only change the color of the cell, and I also need to change the size of the circle, according to the number of fungivores that there are. How can I pass this data to the model, or to differentiate what data is happening to it?
EDIT:
Another problem is that if I modify the values in a loop, for example:
int count = 0; while (count < 3000) { for(int i = 0; i < this->rows; i++) { for(int j = 0; j < this->col; j++) { model->mundo->world[i][j]->setFood(rand() % 255); //model->setData(model->index(i,j),200,Qt::EditRole); qDebug("El ModelIndex[%i][%i] lleva los datos: %i", i, j, model->mundo->world[i][j]->getFood()); } } count++; }
The UI is not updated until the loop completes. It may be that the loop strangles the events ... that's a big problem for me.