update widgets dynamically from threads
-
Hello, I'm making an app composed of several different graphs widgets all made from data stored in an excel file generated by a DLL. When the app is launched it needs to update these graphs in real time when there's new data. The system to know when there's new data available works so this part isn't an issue. However updating the widgets is.
From my limited understanding of Qt (this is my first project using it), the GUI can only be updated from the main thread. So what I was thinking was to have the callback function that fires whenever there's new data emit different signals for each graph to update, have a worker thread do the calculations and generate the new widget and pass it to the GUI thread via a signal.
Is this a good way of solving this issue or is there a cleaner solution?
-
Hello, I'm making an app composed of several different graphs widgets all made from data stored in an excel file generated by a DLL. When the app is launched it needs to update these graphs in real time when there's new data. The system to know when there's new data available works so this part isn't an issue. However updating the widgets is.
From my limited understanding of Qt (this is my first project using it), the GUI can only be updated from the main thread. So what I was thinking was to have the callback function that fires whenever there's new data emit different signals for each graph to update, have a worker thread do the calculations and generate the new widget and pass it to the GUI thread via a signal.
Is this a good way of solving this issue or is there a cleaner solution?
-
@Deneguil Qt way is: emit signals from your threads whenever something needs to be updated in the UI. Connect these signals to slots in GUI thread and update the UI there.
-
@jsulm mhm so I had the right idea with having worker threads do the calculations and sending the updated widget to the GUI thread via a signal. Thank you
@Deneguil
Yes and no. You cannot/must not "sending the updated widget to the GUI thread via a signal". Secondary threads cannot touch Qt UI elements, such as any widgets. They can send the data about what is happening, or what is wanted, but only a slot in the UI thread can do anything about turning that into anything widgety. OK? -
@jsulm mhm so I had the right idea with having worker threads do the calculations and sending the updated widget to the GUI thread via a signal. Thank you
@Deneguil said in update widgets dynamically from threads:
updated widget to the GUI thread via a signal
hold on, QWidgets of any type may only be created in the GUI thread!
You may use your own data struct or class as argument for your signal, but you'll have to register it with the qt metaobject system, when Signal&Slots work across threads.