PDA

View Full Version : Running an external function in a new thread



Andrea Landi
28th May 2011, 18:29
Hi,
I have a problem...in my GUI there is a slot that is executed every 1 ms using a Qtimer and performs image processing through an external function (declared and defined in other files of the project) which takes as input a frame taken from the webcam and some Qlabels of the GUI. It updates the GUI QLabels (the image frame) each time it is called.

The image processing is quite heavy, so, though it is called every 1ms, a new execution (and so a new update of the image frame) of the function takes place after more time than 1 ms.

The program runs quite fine but now, for performance reasons, I would like to run this function in a new thread, separate from the GUI thread (which I didnt explicitly created).

This thread doesnt need to communicate with the GUI thread since all GUI objects of interest (QLabels) are passed as parameters to the function and updated within it.
The only thing I want is that the different executions of the functions does not interfere with eacth other (that is, a new execution should only take place after an execution has been completed). As I said before, this is what happens right now, even if the function is called by the QTimer every 1 ms, the executions dont interfere with each other (I dont know why this happens).

I hope I was clear, I can include the source code if u want to have a look...Can u help me? Thanks

Santosh Reddy
28th May 2011, 20:23
This thread doesnt need to communicate with the GUI thread since all GUI objects of interest (QLabels) are passed as parameters to the function and updated within it
If you run the function in a new thread, it has to be called inside the run() function of the new QThread, you cannot call (and cannot pass arguments), to the function directly from GUI thread.
Moreover you cannot update GUI objects from QThread directly (I mean from the function which executing in QThread)


even if the function is called by the QTimer every 1 ms, the executions dont interfere with each other (I dont know why this happens).
If you processing takes more than 1 ms, the next QTimer event is not delivered. If Qt is unable to deliver the requested number of timer clicks, it will silently discard some

Andrea Landi
29th May 2011, 04:40
Ok..so what I have to do in practice? Please be clear and simple as possible..I am new to Qt and intermediate-advanced C++ programming

Santosh Reddy
29th May 2011, 05:05
If you want to use your existing function in new thread, it will not work as it is, because this function modifies GUI objects (QLabel in you case), as you earlier said.

One important thing to remember when creating new thread in Qt, is never access the GUI objects from new thread, GUI objects should always be in main GUI thread.

So, you need to modify your external function, and separate GUI and logic part of it, then put the logic part of it in the new thread, and put the GUI part of it in the main GUI thread. Then the last thing is to connect you logic to your GUI, that should be straight forward it know how to use signals and slots.

Check this example 6491