Results 1 to 20 of 52

Thread: Qt multithreading and let the second thread update the main (UI) thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #18
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Qt multithreading and let the second thread update the main (UI) thread

    Quote Originally Posted by Cupidvogel View Post
    Qt Code:
    1. //inside the slot
    2.  
    3. //get the query term, and pass it to the action, it will update view through signal/slot, so longer need to pass view as param
    4.  
    5. std::shared_ptr<QueryAction> action = std::make_shared<QueryAction>(queryTerm);
    6.  
    7. //this in turn will run a while loop polling the database, and finish gracefully once polling is complete
    8. action->execute();
    9.  
    10. QThread *t = new QThread();
    11. action->moveToThread(t);
    To copy to clipboard, switch view to plain text mode 
    Not sure what action->execute() is intended to do in line 8. If your intent is to execute something in the new thread, that's not the correct way to accomplish that. For starters, you haven't created the new QThread yet, nor has it been started (QThread::start). Typically you would emit a signal that is connected to a slot in your Worker object, which can be easily done with a QTimer::singleShot.

    The pointer on line 10 is a local pointer, so you will lose the ability to address your QThread after your slot returns. Make the pointer a class member variable, which will allow you to use it later on.

    You should also make all of your signal/slot connections for the Worker object and QThread after line 11.

    Lastly, after you have created the QThread, moved your Worker object to the QThread, and made your signal/slot connections, you need to start the QThread so that it begins running its event loop.

    After you have done all of that, then QTimer::singleShot with a 0ms wait to queue the signal to execute in the new thread, etc.

    Once you have all of that working, you should add a boolean to your Worker object that you can use to tell it to stop processing. i.e. You have started doing a long running task in the new thread and the user wants to cancel the task or close the program, etc. Your long running task would then check the boolean to determine if stop has been requested, etc. To make it all thread safe, you should protect the boolean member variable with a QMutex and QMutexLocker whenever you read or change the boolean value.

    Hope that helps.

  2. The following user says thank you to jefftee for this useful post:

    Cupidvogel (18th April 2015)

Similar Threads

  1. Replies: 1
    Last Post: 28th March 2012, 18:58
  2. Replies: 5
    Last Post: 22nd February 2011, 21:21
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. Replies: 16
    Last Post: 7th October 2009, 08:17
  5. Replies: 6
    Last Post: 29th April 2009, 18:17

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.