PDA

View Full Version : Use Events to update UI



Fivezero05
1st September 2016, 14:54
12104

This picture represents the class structure.

frm => ui
cls => a normal class

So this is what happens :

1) press a button on frmMain
2) clsMainlogic does not really do something itself, but gives the task to clsCameraLogic (other classes are connected to clsMainlogic, but those are not relevant for my question)
3) in clsCameraLogic a function is running some code, but it takes a long time

alright , here is what my problem is:

The function (in clsCameraLogic) takes quite some time to complete. To update the user on its progress, an event is used to update frmLogger.

Now, I know that you can't just update an ui from another thread. So my question : how do I do it?

To complete the cycle:

4) an event from clsCameraLogic with a string is given to clsMainlogic
5) clsMainLogic gives the same data to frmMain
6) frmMain, owner of the frmLogger object, calls a function in frmLogger to update the text in a label

Step 1 to 6 work, the data reaches frmLogger, but it is not updated.

I used the events native to c++, __hook and __unhook .

anda_skoa
1st September 2016, 15:05
The easiest way to report progress from a worker thread to the main thread is to have the code running on the worker thread emit signals and connect them to slots of objects living on the main thread, e.g. UI objects.

Cheers,
_

Fivezero05
1st September 2016, 15:05
welp, guess this is my shortest question ever.

The solution:




ui.my_label->repaint();
qApp->processEvents();


You use this code in frmLogger after you have executed the event.