Stop button is not working.why?
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
ui(new Ui::Dialog)
{
ui->setupUi(this);
mThread = new MyThread(this);
connect(mThread,SIGNAL(NumberChanged(int)),this, SLOT(onNumberChanged (int)) );
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::onNumberChanged(int Number)
{
ui
->label
->setText
(QString::number (Number
));
}
void Dialog::on_pushButton_clicked()
{
//start
mThread->start ();
}
void Dialog::on_pushButton_2_clicked() //This part
{
//stop
mThread->Stop = true;
}
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
mThread = new MyThread(this);
connect(mThread,SIGNAL(NumberChanged(int)),this, SLOT(onNumberChanged (int)) );
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::onNumberChanged(int Number)
{
ui->label->setText(QString::number (Number));
}
void Dialog::on_pushButton_clicked()
{
//start
mThread->start ();
}
void Dialog::on_pushButton_2_clicked() //This part
{
//stop
mThread->Stop = true;
}
To copy to clipboard, switch view to plain text mode
myThread.cpp
#include "mythread.h"
#include<QtCore>
MyThread
::MyThread(QObject *parent
) :{
}
void MyThread::run()
{
for(int i = 0; i < 10000; i++)
{
mutex.lock();
if(this->Stop) //break;
mutex.unlock();
emit NumberChanged (i);
this->msleep (100);
}
}
#include "mythread.h"
#include<QtCore>
MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}
void MyThread::run()
{
for(int i = 0; i < 10000; i++)
{
QMutex mutex;
mutex.lock();
if(this->Stop) //break;
mutex.unlock();
emit NumberChanged (i);
this->msleep (100);
}
}
To copy to clipboard, switch view to plain text mode
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include"mythread.h"
#include <QDialog>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= nullptr
);
~Dialog();
MyThread * mThread;
private:
Ui::Dialog *ui;
public slots:
void onNumberChanged(int);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
};
#endif // DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include"mythread.h"
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
MyThread * mThread;
private:
Ui::Dialog *ui;
public slots:
void onNumberChanged(int);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
Bookmarks