Solved How to achieve high performance asynchronous table model update?
-
Hello,
I have searched high and low for examples, but can't seem to find any good resources.I have a QTableView, and a custom model that is subclassed off of QAbstractTableModel.
I will be pulling data from an external resource. There will be updates to one or many of the rows in the table. There will be about a 1000 rows in the table, with about 5 columns.
I will have a worker thread pulling data from the external source. I understand that one would update the model and fire the corresponding signals for the view to update.
What is the best way to accomplish?
As a row is added in the QTableView, it will get added to the model. Should the worker update the model directly?
JG
-
Keep the model & view in a same thread & update the model here. Pull the data in worker thread. Use signals & slots between threads(worker -> UI ) to update the model. Avoid directly accessing the model directly from Worker thread.
-
Hi,
To add to @dheerendra, you can also do batch updates rather than updating each and every cell every time you have a change.
-
@SGaist Can you please elaborate a little bit on how to do batched updates?
-
If you have a high level of updates, only do emit dataChanged for e.g. 500 samples changed.
-
@SGaist @dheerendra Thanks to both of you for the tips.
-
@dheerendra said in How to achieve high performance asynchronous table model update?:
Use signals & slots between threads(worker -> UI ) to update the model. Avoid directly accessing the model directly from Worker thread.
I beg to differ. The worker should update the data interface, which is the model, not the view. The view shouldn't be known to the worker at all.
-
Another question: In a QTableView using a custom model subclassed off of QAbstractTableModel, how can one set the color of a cell based on comparing new and previous value of the cell? for example, if new value is higher than old set the cell background to green.
-
@JohnGa said in How to achieve high performance asynchronous table model update?:
Another question: In a QTableView using a custom model subclassed off of QAbstractTableModel, how can one set the color of a cell based on comparing new and previous value of the cell? for example, if new value is higher than old set the cell background to green.
You can do that in the QAbstractItemModel::setData override. When you find that you have different data, you can
setData
for the same cell (model index) but withQt::BackgroundRole
and provide a different color. -
@kshegunov wonder where I told to update the view from worker ?
-
@dheerendra said in How to achieve high performance asynchronous table model update?:
@kshegunov wonder where I told to update the view from worker ?
This is how I interpret the following (which I quoted already):
Use signals & slots between threads(worker -> UI ) to update the model.
The model is not UI, and you can update the model through the view, so I imagined this is what you meant.
-
@kshegunov Thanks for clarifications. Looks like my answer is misinterpreted.
Model is not UI & I did not say this also. Model can be updated from View & this is not in the context of his question.Now, worker Thread is pulling the data. Since model is in another thread use signals & slots to update the model which is in another thread. This is the only thing I said in my answer.