Thank you!
Is it right?

Thread_with_terminate.pro
HEADERS += \
threaddialog.h \
mythread.h
SOURCES += \
threaddialog.cpp \
mythread.cpp \
main.cpp
HEADERS += \
threaddialog.h \
mythread.h
SOURCES += \
threaddialog.cpp \
mythread.cpp \
main.cpp
To copy to clipboard, switch view to plain text mode
main.cpp
#include "threaddialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
ThreadDialog w;
w.resize(100, 50);
w.show();
return a.exec();
}
#include "threaddialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ThreadDialog w;
w.resize(100, 50);
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mythread.h
To copy to clipboard, switch view to plain text mode
mythread.cpp
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QtCore>
{
Q_OBJECT
public:
explicit MyThread
(QObject *parent
= 0);
void run();
void stop();
signals:
void numberChanged(int);
public slots:
};
#endif // MYTHREAD_H
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QtCore>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void run();
void stop();
signals:
void numberChanged(int);
public slots:
};
#endif // MYTHREAD_H
To copy to clipboard, switch view to plain text mode
threaddialog.h
#ifndef THREADDIALOG_H
#define THREADDIALOG_H
#include <QDialog>
#include <QtGui>
#include "mythread.h"
class ThreadDialog
: public QDialog{
Q_OBJECT
public:
explicit ThreadDialog
(QWidget *parent
= 0);
signals:
public slots:
void numberChanged(int);
private slots:
void on_startButton_clicked();
void on_stopButton_clicked();
private:
MyThread *myThread;
};
#endif // THREADDIALOG_H
#ifndef THREADDIALOG_H
#define THREADDIALOG_H
#include <QDialog>
#include <QtGui>
#include "mythread.h"
class ThreadDialog : public QDialog
{
Q_OBJECT
public:
explicit ThreadDialog(QWidget *parent = 0);
signals:
public slots:
void numberChanged(int);
private slots:
void on_startButton_clicked();
void on_stopButton_clicked();
private:
QLabel *numberLabel;
MyThread *myThread;
};
#endif // THREADDIALOG_H
To copy to clipboard, switch view to plain text mode
threaddialog.cpp
#include "threaddialog.h"
ThreadDialog
::ThreadDialog(QWidget *parent
) :{
numberLabel
= new QLabel(tr
("Number"));
mainLayout->addWidget(numberLabel);
mainLayout->addStretch();
mainLayout->addWidget(startButton);
mainLayout->addWidget(stopButton);
connect(startButton, SIGNAL(clicked()), this, SLOT(on_startButton_clicked()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(on_stopButton_clicked()));
myThread = new MyThread(this);
connect(myThread, SIGNAL(numberChanged(int)), this, SLOT(numberChanged(int)));
}
void ThreadDialog::on_startButton_clicked()
{
myThread->start();
}
void ThreadDialog::on_stopButton_clicked()
{
myThread->stop();
}
void ThreadDialog::numberChanged(int number)
{
numberLabel
->setText
(QString::number(number
));
}
#include "threaddialog.h"
ThreadDialog::ThreadDialog(QWidget *parent) :
QDialog(parent)
{
numberLabel = new QLabel(tr("Number"));
QPushButton *startButton = new QPushButton(tr("Start"));
QPushButton *stopButton = new QPushButton(tr("Stop"));
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(numberLabel);
mainLayout->addStretch();
mainLayout->addWidget(startButton);
mainLayout->addWidget(stopButton);
connect(startButton, SIGNAL(clicked()), this, SLOT(on_startButton_clicked()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(on_stopButton_clicked()));
myThread = new MyThread(this);
connect(myThread, SIGNAL(numberChanged(int)), this, SLOT(numberChanged(int)));
}
void ThreadDialog::on_startButton_clicked()
{
myThread->start();
}
void ThreadDialog::on_stopButton_clicked()
{
myThread->stop();
}
void ThreadDialog::numberChanged(int number)
{
numberLabel->setText(QString::number(number));
}
To copy to clipboard, switch view to plain text mode
Bookmarks