Qt Update Gui From Worker Thread

  1. Qt Update Gui From Worker Thread For Free
  2. Qt Gui Application
Active5 years, 9 months ago

How can I send an unsigned char buf[10] array from a worker function to a gui thread(main thread).

I was trying to create a signal/slot mechanism where the slot has the buf in the parameters of the function to process it in the gui thread.

UPDATE:

  1. Jan 12, 2019  Update GUI using BackgroundWorker. Of course it's easier to bind to a property and update that from the worker thread. – Stimul8d Mar 4 '11 at 12:10 @Stimul8d, absolutely, and this should be the preferred way of doing things in WPF – Thomas Levesque Mar 4 '11 at 12:43.
  2. In order to do this, the Control class provides the Invoke method, which will take a delegate and execute it on the thread that the UI element was created on. In order to use this, one must. 10 years ago. Reply.NET Compact Framework - Updating the User Interface from a Worker Thread « Mike's Dump.

Those who have discovered the joys of the Qt framework may assume that threads in Qt (QThread) are just like this, but they would be both wrong and right. Then the clean-up: when the worker instance emits finished(), as we did in the example, it will signal the thread to quit, i.e.

this is what I have so far:

worker class:

the worker constructer

the function of the thread

then I start the thread

this is the function in the gui thread which is supposed to process the unsigned char buf[10] from the worker.

Benjamin
17k31 gold badges134 silver badges246 bronze badges
SamuelNLPSamuelNLP
2,1778 gold badges42 silver badges84 bronze badges

1 Answer

The correct way to make the connection is:

connect( worker, SIGNAL(newinfo(unsigned char[10])), this, SLOT(process_new_info(unsigned char[10])) );

However you will then run into the problem that you would be trying to make a queued connection, and Qt doesn't know how to deal with queueing that type. There are ways around that, but you will find it much easier to just put your data into a container such as QByteArray or QVector rather than a raw array.

Dan MilburnDan Milburn
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Qt Update Gui From Worker Thread For Free

Not the answer you're looking for? Browse other questions tagged c++multithreadingqt or ask your own question.

Active7 months ago

This seems like it should be automatic, but apparently it's not. I have the following code:

What is happening is that I never see 'Test1...', as the GUI seems to hang until the following function completes, and I eventaully only see 'Finished.'.

How can I make sure the GUI is updating before it enters that function?
Thanks.

Coding Mash
3,3025 gold badges20 silver badges44 bronze badges
zebrazebra
2,48815 gold badges48 silver badges64 bronze badges

4 Answers

You shuld be able to process the event queue before entering your code if you;

and, when you want to refresh your GUI, call;

Note that it may be a good idea to let your long running process call that function now and then, to make your GUI feel more responsive.

Joachim IsakssonJoachim Isaksson
148k18 gold badges220 silver badges251 bronze badges

If you don't care about your GUI being responsive during this time, then a call to my_label->repaint() would do the trick. Qt can't do anything automatically for you unless you yield to the event loop.

For maximimum responsiveness you might consider running your process in a separate thread and use signal/slot connections (which are thread-safe by default) to signal to your main GUI thread when your processing is complete.

Qt Gui Application

ChrisChrisQt update gui from worker thread
13.3k3 gold badges41 silver badges55 bronze badges

I just wanted to add that for me it took a combo of the two answers I saw here. So what worked for me was:

Hope this helps someone.

Chuck ClaunchChuck Claunch
1,3691 gold badge11 silver badges23 bronze badges

Defining a function...

...like this and make sure this is disabled (to prevent user actions) while you want to force an update of the ui was the best solution for me.

Example how to use it inside a function (for example during a stack processing that takes lots of time):

This doesn't allow the user to disturb the processing by ui interaction (it is disabled) and updates whenever Update_Ui(); is called in the code and updates the whole ui, not just a chosen label or whatever. Note that this doesn't block signals fired by ui elements.

KemendilKemendil

Not the answer you're looking for? Browse other questions tagged c++qtfunctionuser-interface or ask your own question.