Results 1 to 3 of 3

Thread: basic qthread question

  1. #1
    Join Date
    Sep 2008
    Posts
    23
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question basic qthread question

    hi, there
    By clicking a button, I want to do heavy calculation several times in a loop and don't want them block the performance of the GUI, here is what I did. Although it did not block the GUI, it block the loop. How can I do it correct?

    Somewhere in the GUI I have
    Qt Code:
    1. for (int i = 1; i < 10; i++){
    2. cout<<"# "<<i<<endl;
    3. TrashThread trash(this, i);
    4. trash.start();
    5. }
    To copy to clipboard, switch view to plain text mode 

    For the TrashThread class, I have
    Qt Code:
    1. TrashThread::TrashThread(QObject *parent, int i) : QThread(parent)
    2. {
    3. cout<<"thread "<<i<<endl;
    4. }
    5.  
    6. TrashThread::~TrashThread()
    7. {
    8. }
    9. void TrashThread::run()
    10.  
    11. {
    12. cout<<"sleep 5sec"<<endl;
    13. sleep(5);
    14. exec();
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    and here is the result
    Qt Code:
    1. # 0
    2. thread 0
    3. sleep 5sec
    4. //the loop never goes to 2nd iteration
    To copy to clipboard, switch view to plain text mode 
    Thanks for help.
    zl2k

  2. #2
    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: basic qthread question

    You are creating the thread on the stack inside the loop, so the object (and the thread behind it) gets killed before it has a chance to do anything. And you might be experiencing a crash if you never reach the second iteration of the loop.

  3. #3
    Join Date
    Sep 2008
    Posts
    23
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: basic qthread question

    You are right, I now use the signal/slot and QList to hold the trashthread, and each time I can release a small amount of trashthread (the real program takes large memory) each time when some trashthread finished. Thanks.

Similar Threads

  1. Basic question on QComboBox
    By Raccoon29 in forum Newbie
    Replies: 2
    Last Post: 9th May 2008, 16:41
  2. Replies: 2
    Last Post: 21st February 2008, 22:35
  3. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38
  4. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09
  5. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 07:34

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.