Results 1 to 6 of 6

Thread: The right approach to "ask" data to the running thread..

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: The right approach to "ask" data to the running thread..

    I think I got it.. I'll clean the code (I made a mess while tempting) and then I will post here if someone is interested ;-)))

    Many thanks,
    Sylvaticus

  2. #2
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Cool Re: The right approach to "ask" data to the running thread..

    ok.. this is what I done so far.. it took me longer as the first approach (query executed directly by the GUI trough a signal) was working without problems in Linux but randomly crash in Windows..
    Does the following looks as a good approach or it is advisable to be done in an other way??

    I have three classes involved, a MainWindow (QMainWindow derived), ThreadManager (QThread derived) and MainProgram (independent class with the model code).

    MainWindow starts the thread calling QThread::start().

    The QThread::start() call really executes the code in the ThreadManager::run() function that contain MainProgram::run() with all the model code.

    The pause and stop system is implemented with the bool ThreadManager::running and bool ThreadManager::stopped variables.
    Often (every few seconds) the model code goes to check the status of that variables that can be changed also from the GUI. The code is:
    Qt Code:
    1. void
    2. ThreadManager::refreshGUI(){
    3. checkQuery(0,0,false);
    4. while (!running){
    5. if(stopped){
    6. break;
    7. }
    8. }
    9. if (stopped){
    10. emit upgradeLogArea("Model has been stopped.");
    11. running= false;
    12. throw(2);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    checkQuery(int, int, bool) is a function that can be called both from the GUI trough a signal or from the running thread trough refreshGUI(), and it is so protected with a QMutex.
    It's role is to change/control the status of the query parameters that are members of ThreadManager. The third function parameter is a bool that tell the function if the query parameters should be set (if the query come from the GUI) or if the query should be executed and its results sent back to the GUI with a signal (if the call come from the running thread).
    The code is :
    Qt Code:
    1. void
    2. ThreadManager::checkQuery(int px_ID, int currentLayerIndex, bool newRequest){
    3. QMutexLocker locker(&mutex);
    4. if(newRequest){
    5. pxQueryID = px_ID;
    6. layerQueryPos = currentLayerIndex;
    7. if(stopped){computeQuery(pxQueryID, layerQueryPos);layerQueryPos = -1;} // model is stopped, no way the model thread will do the query work
    8. else{emit publishQueryResults("<i>..wait.. processing query..</i>");} // model is running.. it will be the model thread to execute the query
    9. return;
    10. } else {
    11. if(layerQueryPos<0){
    12. return;
    13. } else {
    14. computeQuery(pxQueryID, layerQueryPos);
    15. layerQueryPos = -1;
    16. return;
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    The whole code is located at:
    http://www.regmas.org/doc/referenceM...ainWindow.html
    http://www.regmas.org/doc/referenceM...adManager.html
    http://www.regmas.org/doc/referenceM...inProgram.html

    Thank you.. I think I finally found a pleace to discuss the Qt programming.. the mailing list looks too technical for a beginner like me...

    Cheers...
    Sylvaticus

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: The right approach to "ask" data to the running thread..

    The problem might be in automatic connections. QThread object lives in the GUI thread, so automatic connections between widgets and the thread object will behave like if they were direct, which might cause troubles.

    This problem was discussed several times, so please search the forum. A few examples:
    http://www.qtcentre.org/forum/f-qt-p...eads-6060.html
    http://www.qtcentre.org/forum/f-qt-p...read-3943.html

  4. #4
    Join Date
    Dec 2007
    Location
    Ancona, Italy
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: The right approach to "ask" data to the running thread..

    Thanks for the explanation & the links.....

Similar Threads

  1. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 06:27
  4. Replies: 10
    Last Post: 20th March 2007, 22:19

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
  •  
Qt is a trademark of The Qt Company.