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. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

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

    So I have a view (say QFrame) in the main thread (currently there is only a single thread where the entire application resides), where I need to show some query results when the user enters some query term in the text box and presses Enter. This is how it works, I have class called QueryFinder which I instantiate with the query term. Then I call execute(), which polls some internal database for some time to get query results. While instantiating QueryFinder, I also pass the view (the QFrame from which the user queried) and that is held as a view object in the QueryFinder class. Whenever any query result match is found, I call something like fView->updateView(searchResult), and the view has the appropriate method update to show the result. The results keep on coming until the database polling is complete.

    Now the database polling is pretty memory intensive, so while the querying goes on, the UI becomes kind of unresponsive. A loading gif image, for example, which signifies the ongoing querying, often stalls and freezes. So I want to separate out the entire querying part into a separate thread, and that thread should poll, and update the view (which is in a separate thread, the main one). How do I do this in Qt/C++? Here is my pseudocode:

    Qt Code:
    1. //view code
    2. //after user presses enter
    3. if (!finder)
    4. {
    5. finder->stop();
    6. }
    7. finder = new QueryFinder(this, queryTerm);
    8. finder->execute();
    9.  
    10. void updateView(QueryResult &result)
    11. {
    12. //display the query result properly
    13. }
    14.  
    15. //QueryFinder code, header file
    16. class QueryFinder: public QObject
    17. {
    18. public:
    19. QUeryFinder(QWidget *, QueryTerm&);
    20. void execute();
    21. void stop();
    22.  
    23. private:
    24. bool shouldStop;
    25. QWidget *fView;
    26. QueryTerm fTerm;
    27. }
    28.  
    29.  
    30. //QueryFinder code, implementation file
    31. QueryFinder::QueryFinder(QWidget *w, QueryTerm& term): fView(w), fTerm(term)
    32. {
    33. shouldStop = false;
    34. }
    35.  
    36. void QueryFinder::execute()
    37. {
    38. //do the polling and if any result found, say newResult, update view, but only if no new query request has come in
    39. //the meantime, else abnson this object altogether
    40. while (cursor->hasNext()) {
    41. newResult = cursor->getCurResult();
    42. if (!shouldStop)
    43. fView->updateView(newResult);
    44. else
    45. return;
    46. }
    47. }
    48.  
    49. void QueryFinder::stop()
    50. {
    51. shouldStop = true;
    52. }
    To copy to clipboard, switch view to plain text mode 

    If you observe, not only the updating the view, even the stop method needs to go from one thread to another. How do I accomplish this? I have never done any multithreading in Qt/C++.
    Last edited by Cupidvogel; 18th April 2015 at 00:33.

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.