PDA

View Full Version : Do I need update model in QT UI thread?



ysilent
22nd March 2012, 07:45
Hi guys,

I was told that if you want to update the model of a widget, you need do it in QT thread, means you need emit a signal and update model in slot. Is that true?

The problem I faced is I change a tree view's model in a thread, but during I updating the model, QT invoke some draw process.
Because the model is deleted, some value is not available, some index is incorrect, then GUI crashed.

Then I found some function "beginResetModel", "endResetModel". I put them when I update the model.
Some times the QT still call draw process and crash the GUI.

Any comments?

wysota
22nd March 2012, 09:08
If two threads access the same data, their access needs to be synchronized with mutexes or similar mechanisms.

ysilent
23rd March 2012, 01:56
Thanks, but nothing to do with my question.

My question is what is the standard way when you want to destroy all model and reset the widget?
You mean I need override all draw method and add mutex on all of them?

It's always happen, very common question.
When you design a real product you will have some data object and show these data on GUI. Every time these data can be totally different, so you have to delete them all and create the tree again. QT should have some mechanism to avoid painting during model is unavailable, right?

wysota
23rd March 2012, 10:59
Thanks, but nothing to do with my question.
On the contrary, everything to do with your question.


My question is what is the standard way when you want to destroy all model and reset the widget?
You mean I need override all draw method and add mutex on all of them?
No, I mean that if you want to change the model from within a worker thread, you need to synchronize this thread with the main thread so that you're not modifying internal structure of the model while the main thread reads data from the model.
Thus you should wrap all access to those internal structures in mutexes.


QT should have some mechanism to avoid painting during model is unavailable, right?
The model is always available. If you want, you can create the new structure outside the model and then only replace the old structure with the new one and reset the model. The latter has to be done from the main thread, the former (rebuilding the model structure) can be done from a worker thread. Both threads need to be synchronized when the internal data is to be replaced.