Results 1 to 7 of 7

Thread: terminate a QThread from GUI

  1. #1
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default terminate a QThread from GUI

    Hello guys,

    I'm learning QThread with a simple application ... my thread start with a modified Run() function.

    in the run(), l activate a QDialog, do some stuff and wait with exec() for the user to click on QPushbutton "Keep" or "close"

    Qt Code:
    1. Thread_PulseMeas::Thread_PulseMeas(QObject *parent):
    2. QThread(parent)
    3. {
    4. diag_RealTimePlot = new QDialog;
    5. ui_RealTimePlot.setupUi(diag_RealTimePlot);
    6. diag_RealTimePlot->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
    7. connect(ui_RealTimePlot.Close, SIGNAL(clicked()), this, SLOT(Close()));
    8. connect(ui_RealTimePlot.Add, SIGNAL(clicked()), this, SLOT(Add()));
    9. };
    10.  
    11. void Thread_PulseMeas::run( Pulse_Meas *PulseMeas)
    12. {
    13. diag_RealTimePlot->show();
    14. Pulse_measure(PulseMeas);
    15. exec();
    16.  
    17. }
    18.  
    19. void Thread_PulseMeas::Close()
    20. {
    21. SetKeep(false);
    22. diag_RealTimePlot->close();
    23. exit();
    24. }
    25. void Thread_PulseMeas::Add()
    26. {
    27. SetKeep(true);
    28. diag_RealTimePlot->close();
    29. exit();
    30. }
    To copy to clipboard, switch view to plain text mode 
    when l click "keep" or "close", the QDIALOG is closed but the thread is not terminated ....

    am l missing something there ?

    regards,

    Michael
    Last edited by wysota; 24th July 2010 at 22:41. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: terminate a QThread from GUI

    How do you check if the thread is terminated?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: terminate a QThread from GUI

    You can't access GUI from threads!!!
    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.


  4. #4
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: terminate a QThread from GUI

    from my main program where l started the thread with run() ... it is blocked at run()....if l replace exec() with quit() in the thread::run() ....it is okay but then l can not manage the signal Keep or Close in my thread

  5. #5
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: terminate a QThread from GUI

    Wysota ...

    my GUI is a just a QDIALOg l mange in the Thread ...seems to work well, eexcept to exit the thread when l use signal... l read around in this forum there is limitation on calling Signal in a thread, but it is not really clear to me !!

    M.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: terminate a QThread from GUI

    Quote Originally Posted by OverTheOCean View Post
    from my main program where l started the thread with run()
    Are you calling run() explicitly? Then you are not starting a thread. You should be calling start(), not run().

    Quote Originally Posted by OverTheOCean View Post
    my GUI is a just a QDIALOg l mange in the Thread ...seems to work well, eexcept to exit the thread when l use signal...
    Seems you don't have a thread at all. But if you did, your code would crash while trying to access the dialog.
    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.


  7. #7
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: terminate a QThread from GUI

    hey Wysota,

    well l reworked my code so the GUI is manage outside the thread and l update the GUI using Signal in the Thread...and now use Start() to activate the thread.

    everything works as l want.

    thanks for your help,

    Michael

Similar Threads

  1. Best way to terminate a copy thread?
    By scarleton in forum Newbie
    Replies: 1
    Last Post: 13th July 2010, 06:43
  2. Terminate a QThread with an event loop
    By paolom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 12:53
  3. QThread::terminate () is not working!!!!!
    By biswajithit in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2008, 13:03
  4. How to destroy or terminate a thread ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 26th December 2006, 17:44
  5. Replies: 4
    Last Post: 27th July 2006, 12:13

Tags for this Thread

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.