Results 1 to 4 of 4

Thread: Thread design advice needed

  1. #1
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Thread design advice needed

    My question is inside the run function below

    Qt Code:
    1. class TestObject : public QThread
    2. {
    3. Q_OBJECT
    4. private:
    5. QNetworkReply *m_reply;
    6. QUrl m_url;
    7.  
    8. public slots:
    9. void gotNetworkHeader()
    10. {
    11. // do something
    12. }
    13.  
    14. public:
    15. TestObject(QUrl url) : m_url(url)
    16. {
    17. }
    18.  
    19. void run()
    20. {
    21. QNetworkAccessManager nam;
    22. m_reply = nam.head(QNetworkRequest(m_url));
    23. connect(m_reply, SIGNAL(finished()), this, SLOT(gotNetworkHeader()));
    24.  
    25. //
    26. // what do I do here to stop the thread from
    27. // exiting until gotNetworkHeader has finished
    28. // execution?
    29. //
    30.  
    31. // I've tried exec(), but this seems to block QNetworkAccessManager
    32.  
    33. }
    34. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by jonks; 22nd October 2009 at 05:47.

  2. #2
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Thread design advice needed

    I just stumbled across this, which has some examples which are completely different to my design, but do exactly what I want.

    http://doc.trolltech.com/qq/qq27-responsive-guis.html

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread design advice needed

    simple
    assign a private bool function
    private:
    bool stop;

    in your constructor
    stopped = false; //declare it
    also use connect here itself in constructor

    in run () function
    Qt Code:
    1. while(stop)
    2. {
    3. //busy line ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    and in private slot

    Qt Code:
    1. gotNetworkHeader()
    2. {
    3. stop = true;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd October 2009 at 22:33. Reason: missing [code] tags
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Thread design advice needed

    Actually exec() would do the job. Just remember the QThread object lives in the main thread and not in the thread it controls.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Advice needed regarding GUI and external thread cooperation
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 17th June 2009, 01:10
  2. Thread Design Question
    By craigdillabaugh in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 20:16
  3. QTableWidget Design & Thread Safety Question
    By bpetty in forum Qt Programming
    Replies: 4
    Last Post: 28th March 2008, 01:09
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. Replies: 10
    Last Post: 20th March 2007, 23: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.