第一种方法,QThread自身并不生存在它run函数所在的线程,而是生存在旧的线程中,线程无法接收信号,只能发送信号。和第一种方法比较,第二种方法使用movetothread更加灵活。
在qt中新建widget项目,新建MyThread类。
mythread.h 新建mythread继承QObject
#ifndef WORKOBJECT_H
#define WORKOBJECT_H
#include
#include
#include
class MyThread : public QObject
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
signals:
public slots:
void doWork();
};
#endif // WORKOBJECT_H
mythread.cpp
#include "mythread.h"
#include
#include
MyThread::MyThread(QObject *parent) : QObject(parent)
{
}
void MyThread::doWork()
{
while (1)
{
qDebug()<<tr("mythread QThread::currentThreadId()==")<<QThread::currentThreadId();
qDebug()<<"66";
QThread::sleep(1);
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#pragma execution_character_set("utf-8")
#include
#include
#include
#include
#include
#include
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void createView();
Ui::Widget *ui;
Ui::Widget *getUi() const;
void setUi(Ui::Widget *newUi);
signals:
void uiChanged();
private slots:
void openThreadBtnSlot();
void closeThreadBtnSlot();
void finishedThreadBtnSlot();
// void testBtnSlot();
private:
QVBoxLayout *mainLayout;
MyThread *thread1;
QThread* thread = new QThread();
MyThread* mythread = new MyThread();
Q_PROPERTY(Ui::Widget * ui READ getUi WRITE setUi NOTIFY uiChanged)
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
// QThread* thread = new QThread();
// MyThread* mythread = new MyThread();
// QObject::connect(thread, SIGNAL(started()), mythread, SLOT(doWork()));
// QObject::connect(thread, SIGNAL(finished()), mythread, SLOT(deleteLater()));
//createView();
connect(ui->openThreadBtn,SIGNAL(clicked(bool)),this,SLOT(openThreadBtnSlot()));
connect(ui->closeThreadBtn,SIGNAL(clicked(bool)),this,SLOT(closeThreadBtnSlot()));
//connect(thread1,SIGNAL(finished()),this,SLOT(finishedThreadBtnSlot()));
}
void Widget::createView()
{
}
Ui::Widget *Widget::getUi() const
{
return ui;
}
void Widget::setUi(Ui::Widget *newUi)
{
if (ui == newUi)
return;
ui = newUi;
emit uiChanged();
}
void Widget::openThreadBtnSlot()
{
qDebug() << "thread id:" << QThread::currentThreadId();
mythread->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), mythread, SLOT(doWork()));
thread->start();
}
void Widget::closeThreadBtnSlot()
{
thread->quit();
thread->wait();
//mythread->deleteLater();
}
void Widget::finishedThreadBtnSlot()
{
qDebug()<<tr("完成信号finished触发");
}
Widget::~Widget()
{
}
main.cpp
#include "widget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
两种多线程方式对比: