Results 1 to 20 of 52

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

Hybrid 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 Re: Qt multithreading and let the second thread update the main (UI) thread

    Umm, wait, why would I ask you for help if I considered you stupid? I forgot to remove the SignalClass from that part of the code because I was editing my copy paste from my previous post and overlooked that part. Obviously this is not the exact code I have, there are lots of variable definitions, array populating, my project specific code (which I am forbidden to post for confidentiality reasons) etc which don't belong to the ambit of this problem and thus I did not include here (for starters, my QueryAction header class is 8 lines long, in reality it is around 35~40 lines.) Fo example after the querying is complete I have the action class emit a signal which feeds in to a slot in the view that shows a message like 'Total number of query results is <some_number>', but I did not include it here because it doesn't add anything to the problem scope. I have changed some of my variable names here so that I had to do less typing (for example I renamed fSearchAction to action). It's only after you said that the Signal Class mapper might be causing the problem did I go ahead and change it (while editing I forgot to edit one part). In essence this is the summary of the entire process I am currently running. Why on earth would I not try out what you are suggesting and yet ask you for further suggestions?

    If it helps, here is my latest code (statutory disclaimer: this code cannot be and is not considered completely representative of my entire project, not does it purport to be. It's aim is to provide a high level overview of my problem, and is open to request [please] for more information, that will be provided subject to the terms and conditions of the company I am working for in capacity of a software engineer to make a product, with an ardent expectation that it will be sufficient to experienced Qt programmers in this forum to be able to offer advice and suggestion to improve it. Viewers' discretion is advised.)

    QueryView.hpp:

    Qt Code:
    1. class QueryView
    2. {
    3. QThread *t;
    4. QueryAction *action;
    5. signals:
    6. void excuteSignal();
    7. void stopExistingQuery();
    8. }
    To copy to clipboard, switch view to plain text mode 

    QueryView.cpp:

    Qt Code:
    1. QueryView::QueryView()
    2. {
    3. connect(queryButton, SIGNAL(clicked()),this, SLOT(doQuery()));
    4. }
    5.  
    6. void QueryView::doQuery()
    7. {
    8. t = new QThread()
    9. emit stopExistingQuery();
    10. action = new QueryAction(queryTerm);
    11.  
    12. connect(this,SIGNAL(excuteSignal()), action, SLOT(startQuery()));
    13. connect(this,SIGNAL(stopExistingQuery()), action, SLOT(stopQuery()));
    14. connect(action, SIGNAL(updateViewSignal(QString)),this, SLOT(updateView(QString)));
    15.  
    16. action->moveToThread(t);
    17. t->start();
    18. emit excuteSignal();
    19.  
    20. }
    21.  
    22. QueryView::updateView(QString term)
    23. {
    24. ...update view with query result, currently a string for testing purpose, as a QPushButton, forming a series of buttons in a QTableWidget
    25. }
    To copy to clipboard, switch view to plain text mode 

    QueryAction.hpp:

    Qt Code:
    1. class QueryAction
    2.  
    3. {
    4. QMutex m_mutex;
    5.  
    6. signals:
    7. void updateViewSignal(QString);
    8. }
    To copy to clipboard, switch view to plain text mode 

    QueryAction.cpp:

    Qt Code:
    1. QueryAction::QueryAction(QueryTerm& queryTerm): fQueryTerm(queryTerm)
    2. {
    3. QMutexLocker locker(&m_mutex);
    4. continueQuery = true;
    5. }
    6.  
    7. void QueryAction::startQuery()
    8. {
    9. //init cursor
    10. while (db_cursor != empty)
    11. {
    12. ...get query result
    13. QMutexLocker locker(&m_mutex);
    14. if (continueQuery)
    15. emit updateViewSignal(aResult.asQString());
    16. else
    17. break;
    18. }
    19. }
    20.  
    21. void QueryAction::stopQuery()
    22. {
    23. QMutexLocker locker(&m_mutex);
    24. continueQuery = false;
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cupidvogel; 21st April 2015 at 12:34.

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.