继承QThread
效果图
源码
threaddlg.h
#ifndef THREADDLG_H
#define THREADDLG_H
#include <QDialog>
#include <QPushButton>
#include "workthread.h"
#define MAXSIZE 5
class ThreadDlg : public QDialog
{
Q_OBJECT
public:
ThreadDlg(QWidget *parent = 0);
~ThreadDlg();
private:
QPushButton *startBtn;//开始
QPushButton *stopBtn;//停止
QPushButton *quitBtn;//退出
public slots:
void slotStart();//开始响应
void slotStop();//停止
private:
WorkThread *workThread[MAXSIZE];//线程数组
};
#endif // THREADDLG_H
threaddlg.cpp
#include "threaddlg.h"
#include <QHBoxLayout>
ThreadDlg::ThreadDlg(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("线程"));
startBtn = new QPushButton(tr("开始"));
stopBtn = new QPushButton(tr("停止"));
quitBtn = new QPushButton(tr("退出"));
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(startBtn);
mainLayout->addWidget(stopBtn);
mainLayout->addWidget(quitBtn);
connect(startBtn,SIGNAL(clicked()),this,SLOT(slotStart()));
connect(stopBtn,SIGNAL(clicked()),this,SLOT(slotStop()));
connect(quitBtn,SIGNAL(clicked()),this,SLOT(close()));
}
ThreadDlg::~ThreadDlg()
{
}
void ThreadDlg::slotStart()
{
for(int i=0;i<MAXSIZE;i++)
{
workThread[i]=new WorkThread();
}
for(int i=0;i<MAXSIZE;i++)
{
workThread[i]->start();//线程开始
}
startBtn->setEnabled(false);
stopBtn->setEnabled(true);
}
void ThreadDlg::slotStop()
{
for(int i=0;i<MAXSIZE;i++)
{
workThread[i]->terminate();//终止
workThread[i]->wait();//等待
}
startBtn->setEnabled(true);
stopBtn->setEnabled(false);
}
workthread.h
#ifndef WORKTHREAD_H
#define WORKTHREAD_H
#include <QThread>
class WorkThread : public QThread
{
Q_OBJECT
public:
WorkThread();
protected:
void run();//线程开始后执行的函数
};
#endif // WORKTHREAD_H
workthread.cpp
#include "workthread.h"
#include <QtDebug>
WorkThread::WorkThread()
{
}
void WorkThread::run()
{
while(true)
{
for(int n=0;n<10;n++)
qDebug()<<n<<n<<n<<n<<n<<n<<n<<n;
}
}
2.应用示例
继承QThread的类
#ifndef MYTHREADTEST_H
#define MYTHREADTEST_H
#include <QThread>
class MyThreadTest : public QThread
{
Q_OBJECT
public:
MyThreadTest();
//获取返回的信息
QString* getStrInfo();
//设置返回的信息
void setStrInfo(QString str1);
protected:
//start后执行的函数
void run();
signals:
//线程run完后发送的信号
void ThreadOver(QString);
private:
//返回的信息
QString m_strInfo;
};
#endif // MYTHREADTEST_H
#include "mythreadtest.h"
MyThreadTest::MyThreadTest()
{
m_strInfo = "hello";
}
QString *MyThreadTest::getStrInfo()
{
return &m_strInfo;
}
void MyThreadTest::setStrInfo(QString str1)
{
m_strInfo = str1;
}
void MyThreadTest::run()
{
emit ThreadOver(m_strInfo);
}
调用示例
m_testThread = new MyThreadTest;
//设置线程执行完成返回的信息
m_testThread->setStrInfo("gg");
//连接线程执行完成的信号和槽
connect(m_testThread,SIGNAL(ThreadOver(QString)),this,SLOT(slotThreadOver(QString)));
//线程开始执行
m_testThread->start();
//接收到线程执行结束响应的槽函数
void MainWindow::slotThreadOver(QString str1)
{
ui->pushButton->setText(*(m_testThread->getStrInfo()));
}
通过moveToThread调用
原文url:https://subingwen.cn/qt/thread/#3-1-%E6%93%8D%E4%BD%9C%E6%AD%A5%E9%AA%A4
mywork.h
#ifndef MYWORK_H
#define MYWORK_H
#include <QObject>
class MyWork : public QObject
{
Q_OBJECT
public:
explicit MyWork(QObject *parent = nullptr);
// 工作函数
void working();
signals:
void curNumber(int num);
public slots:
};
#endif // MYWORK_H
mywork.cpp
#include "mywork.h"
#include <QDebug>
#include <QThread>
MyWork::MyWork(QObject *parent) : QObject(parent)
{
}
void MyWork::working()
{
qDebug() << "当前线程对象的地址: " << QThread::currentThread();
int num = 0;
while(1)
{
emit curNumber(num++);
if(num == 10000000)
{
break;
}
QThread::usleep(1);
}
qDebug() << "run() 执行完毕, 子线程退出...";
}
调用:
// 创建线程对象
QThread* sub = new QThread;
// 创建工作的类对象
// 千万不要指定给创建的对象指定父对象
// 如果指定了: QObject::moveToThread: Cannot move objects with a parent
MyWork* work = new MyWork;
// 将工作的类对象移动到创建的子线程对象中
work->moveToThread(sub);
// 启动线程
sub->start();
// 让工作的对象开始工作, 点击开始按钮, 开始工作
connect(ui->startBtn, &QPushButton::clicked, work, &MyWork::working);
// 显示数据
connect(work, &MyWork::curNumber, this, [=](int num)
{
ui->label->setNum(num);
});