Results 1 to 4 of 4

Thread: Running an external function in a new thread

  1. #1
    Join Date
    May 2011
    Location
    Italy
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Running an external function in a new thread

    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
    In QT I believe

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Running an external function in a new thread

    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

  3. #3
    Join Date
    May 2011
    Location
    Italy
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Running an external function in a new thread

    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
    In QT I believe

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Running an external function in a new thread

    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 ThreadTest.zip
    Last edited by Santosh Reddy; 29th May 2011 at 05:54. Reason: updated contents

Similar Threads

  1. Replies: 2
    Last Post: 5th August 2010, 11:18
  2. Replies: 10
    Last Post: 1st July 2010, 11:59
  3. Replies: 5
    Last Post: 17th March 2010, 19:30
  4. External libary in a separate thread: is this correct?
    By sylvaticus in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2009, 09:47
  5. running external applications via QT?
    By cruisx in forum Newbie
    Replies: 1
    Last Post: 11th August 2009, 07:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.