Results 1 to 5 of 5

Thread: QThread stop button is not working

  1. #1
    Join Date
    Jul 2018
    Posts
    7
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default QThread stop button is not working

    Stop button is not working.why?

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4.  
    5. Dialog::Dialog(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::Dialog)
    8. {
    9. ui->setupUi(this);
    10. mThread = new MyThread(this);
    11. connect(mThread,SIGNAL(NumberChanged(int)),this, SLOT(onNumberChanged (int)) );
    12. }
    13.  
    14. Dialog::~Dialog()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void Dialog::onNumberChanged(int Number)
    20. {
    21. ui->label->setText(QString::number (Number));
    22. }
    23.  
    24. void Dialog::on_pushButton_clicked()
    25. {
    26. //start
    27. mThread->start ();
    28. }
    29.  
    30. void Dialog::on_pushButton_2_clicked() //This part
    31. {
    32. //stop
    33. mThread->Stop = true;
    34. }
    To copy to clipboard, switch view to plain text mode 

    myThread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include<QtCore>
    3.  
    4. MyThread::MyThread(QObject *parent) :
    5. QThread(parent)
    6. {
    7.  
    8. }
    9.  
    10. void MyThread::run()
    11. {
    12. for(int i = 0; i < 10000; i++)
    13. {
    14. QMutex mutex;
    15. mutex.lock();
    16. if(this->Stop) //break;
    17. mutex.unlock();
    18.  
    19. emit NumberChanged (i);
    20.  
    21. this->msleep (100);
    22. }
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include"mythread.h"
    5. #include <QDialog>
    6.  
    7. namespace Ui {
    8. class Dialog;
    9. }
    10.  
    11. class Dialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Dialog(QWidget *parent = nullptr);
    17. ~Dialog();
    18. MyThread * mThread;
    19. private:
    20. Ui::Dialog *ui;
    21.  
    22. public slots:
    23. void onNumberChanged(int);
    24. private slots:
    25. void on_pushButton_clicked();
    26. void on_pushButton_2_clicked();
    27.  
    28. };
    29.  
    30. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QThread stop button is not working

    Hi,

    Break is commented out. Is that what you want do?

    Sincerely

  3. #3
    Join Date
    Jul 2018
    Posts
    7
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QThread stop button is not working

    Quote Originally Posted by nix View Post
    Hi,

    Break is commented out. Is that what you want do?

    Sincerely

    when i write break it gives me an error QMutex: destroying locked mutex thats why i comment that

  4. #4
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QThread stop button is not working

    The warning is right, you are exiting the run function after the break statement, destroying the mutex in the process without unlocking it.

    I don't know what you try to achieve with the mutex but used like that is useless, if it can help you can take a look at qmutexlocker.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QThread stop button is not working

    Stop button is not working.why?
    1 - You have not actually created a thread. You have created a QThread instance in the GUI thread. This does not create an independently executing thread, and calling start() simply results in your MyThread's run() method being called.
    2 - Once run() starts, then you are locked in an infinite loop. Calling sleep() or msleep() puts your entire application to sleep for that period of time since you have only one thread running (the GUI thread).
    3 - Any time you have a loop that runs for a long time where you do not allow the Qt event loop to process new events, then your application will freeze up. If you have only a single thread, then you must allow control to return to the event loop. One way is to call QCoreApplication::processEvents(). This is why your button click doesn't work.

    I'd suggest you read Maya Posch's excellent blog post on multithreading and QThread. Or buy her book.
    Last edited by d_stranz; 18th July 2018 at 22:27.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. can not stop QThread
    By newcfd in forum Qt Programming
    Replies: 7
    Last Post: 15th December 2015, 11:03
  2. QThread : how to stop an infinite loop
    By TarielVincent in forum Qt Programming
    Replies: 9
    Last Post: 24th February 2012, 22:22
  3. Moving QObject to QThread causes signals to stop working
    By Ban-chan in forum Qt Programming
    Replies: 8
    Last Post: 13th July 2010, 22:39
  4. How to stop QThread?
    By vespasianvs in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2010, 07:42
  5. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 08:51

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.