Solved Insert fill circle into cell of QTableWidget
-
@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.
-
@juaniyoalm said in Insert fill circle into cell of QTableWidget:
How can I pass this data to the model, or to differentiate what data is happening to it?
But the model has already access to the Cell structure. ( via model)
So delegate can read the information and calculate the size of circle with more.Regarding the loop.
Yes, i think the loop will strangle the event loop as 3000 x row x col might take some time.
One option is to use QCoreApplication::processEvents in the loop but thats not so pretty.
It would be better to use a timer to change the food over time and keep the UI responsive. -
But the delegate receives the data of the model and this only has the value of the food (color).
Regarding the timer. I need not delay the execution time, because later I must see the difference between parallel version of the app and this version.
-
As long as your model stores all the data you need, your delegate can access them. Use custom roles for these additional informations. This has the added advantage to make your code easier to read and understand.
-
If I put QCoreApplication::processEvents(); the UI is not updated until, for example, I do not select the cells in the UI. I need them to be updated automatically when I change the values from a method of the world class. I can not use timer and the values change each time range because this application will later have a parallel version and what is searched in that app is the simulation of a real biological environment as quickly as possible. I need a solution, because I'm delaying too much at the point of the UI (it's the least important).
I want to thank you for all the help you have given me, but I need to solve this point.
The method that will modify the values is not this, but it will look like this:
void World::initializeWorld() { int count = 0; while (count < 1500) { for(int i = 0; i < rowSize(); i++) { for(int j = 0; j < rowSize(); j++) { getCell(i,j)->setFood(rand()&255); } } count++; QCoreApplication::processEvents(); } }
I put the updated project link in case you want to look at it:
-
@juaniyoalm
Hi
The timer is meant as a way of not strangulating the event loop.
its not as fast a tight loop as it goes over the event loop but it
allows redraw of the view. ( which also uses the event loop to actually redraw)
Anyway, you are looping row x colSize ( actually rox x row ??)
and then allow event loop.
maybe you mean like this ?while (count < 1500) { for(int i = 0; i < rowSize(); i++) { for(int j = 0; j < rowSize(); j++) { // should this not be colSize ? getCell(i,j)->setFood(rand()&255); QCoreApplication::processEvents(); } } count++; }
-
@mrjj said in Insert fill circle into cell of QTableWidget:
QCoreApplication::processEvents();
Row x Row is because the rows will really be the same as the columns.
I have also tried to locate QCoreApplication :: processEvents (); in that position, with the same effect, that is, it does not update well in the UI.
If the timer does not penalize the execution time of the program and only acts in the reprssentación of the UI, it could be good. How should I do it?
-
Hi
- it does not update well in the UI.
meaning it does update sometimes or what result do you get ?
here is sample of changing random cell
int random(int min, int max) //range : [min, max) { static bool first = true; if (first) { srand( time(nullptr) ); //seeding for the first time only! first = false; } return min + rand() % (( max + 1 ) - min); } void MainWindow::changeVal() { //model->mundo->world[4][4]->setFood(200); auto timer = new QTimer(this); QObject::connect(timer, &QTimer::timeout, [this]() { int rowCount = this->model->rowCount(QModelIndex()); int colCount = this->model->columnCount(QModelIndex()); this->model->setData(model->index(random(0, rowCount), random(0, colCount)), random(0, 20), Qt::EditRole); }); timer->start(10); } Note that sample will create a timer each time you select Change val menu so its better to move timer as member. Also it uses a lambda as slot. you can use a normal slot for it and let it be part of World class.
- If the timer does not penalize the execution time of the program
it does to some degree. But even using Threads, you will have to ask it to update and redraw
over the event loop as the VIEW cannot update if you strangulate it using tight loops.
But using timer is not super slow.
(video is actually slower than real deal) -
meaning it does update sometimes or what result do you get ?
Means that the UI is only updated if I interact with it, that is, if for example I select the cells, then it shows the changes made in the code.
But using timer is not super slow
Yes but in the parallel version it should go faster and if I use a timer, it will always take the same time, because the time depends on the timer variable.
Video with an example of what happens to me:
Change button calls the InitializeWorld() method.
-
@juaniyoalm said in Insert fill circle into cell of QTableWidget:
- it will always take the same time, because the time depends on the timer variable.
Not really. the timer will call your function at a fix time. Your function will take the time it takes but using a timer, its all driven like a loop but allows the event to run to the view can update.
Anyway, what you are seeing is expected.
You are modifying the vector list BEHIND models back. It dont know you changed some of its data so first when you click something, view re-reads the cells and draws the values.Recall the setData.
bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role) { auto RowCount = this->mundo->world.size(); auto ColCount = this->mundo->world[0].size(); qDebug () << "role=" << role; if (index.isValid() && role == Qt::EditRole && index.row() < RowCount && index.row() >= 0 && index.column() < ColCount && 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; }
here we emit dataChanged so view knows something changed.
when you do
getCell(i,j)->setFood(rand()&255);
you are not telling model/view you changed the list and that is why
QCoreApplication::processEvents(); do not help. -
But if you modified only the value of a cell if work fine.
How could I then modify the value? It has to be modified from the world class. Could I put a signal when modified with the setdata method?
-
@juaniyoalm
Hi
You can add add signal to World class and connect it to the model. and then emit it
to have model call DataChanged. OR give World a pointer to the model and use models
API to change data and inform view.- But if you modified only the value of a cell if work fine.
im not sure what u say here.
- But if you modified only the value of a cell if work fine.
-
@mrjj
I want to say that if I do thisgetCell(0,0)->setFood(rand()&255);
The UI update fine and model unknown data either
-
@juaniyoalm
Ok. not really sure how,
but didnt check the latest version.
Also if the VIEW get an update/paintEvent it will re-read the indexes so i guess
its via the normal paint/update/redraw. -
I have already solved the problem. I have created a signal in the Cell class that is emited when the value of the cell is modified. This signal sends the cell to the slots. Then I created a slot in the MainView class and connected each cell of the model's matrix with that slot. This slot creates a QModelIndex and issues a dataChanged for the model to update the view. Besides, I had to save the position of the cell in the cell class.
Emit Signal:
void Cell::setFood(int food) { if(this->fungus != food) { this->fungus = food; emit this->updateModel(this); } }
Slot:
void MainWindow::foodChanged(Cell *cell) { QModelIndex aux = model->index(cell->place.first, cell->place.second); model->dataChanged(aux, aux); }
Connections:
for (int i = 0; i < this->rows; i++) { for (int j = 0; j < this->columns; j++) { connect(this->model->mundo->getCell(i, j), &Cell::updateModel, this, &MainWindow::foodChanged); } }
Now I need pass the value of the circle also in the data method so that the delegate's paint method can receive it ...
Any example of how to do it? -
As I already suggested many times: use custom roles. So in your paint function, request the data using the roles specific to the parameters you want to get.
-
Yes, but I unknown how to do it...
-
QVariant data = model->data(MyModel::MyCustomRole);
In your model:
QVariant MyModelModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const { // usual stuff if (role == MyModel::MyCustomRole) { return mound->getCell(index.row(), index.column())->myCoolProperty; } }