Qt线程简介
Qt提供了QThread类表示线程
常用函数
start,调用系统API创建线程,新线程创建出来后会自动执行run函数
finish,线程结束时会发出信号,可以通过该信号来实现线程的清理工作
在Qt中,为了防止多线程修改界面的不安全现象,Qt选择一刀切,对于界面的控件状态修改必须在主线程中进行
分为三个部分,公有函数、公有槽函数,信号,Qt提供的静态成员函数
父对象和父类是两回事,父类是继承关系,父对象是从属关系
QThread::QThread(QObject *parent = Q_NULLPTR);
指定一个父对象就可以了
bool QThread::isFinished() const;
bool QThread::isRunning() const;
Priority QThread::priority() const;
void QThread::setPriority(Priority priority);
优先级:
QThread::IdlePriority --> 最低的优先级
QThread::LowestPriority
QThread::LowPriority
QThread::NormalPriority
QThread::HighPriority
QThread::HighestPriority
QThread::TimeCriticalPriority --> 最高的优先级
QThread::InheritPriority --> 子线程和其父线程的优先级相同, 默认是这个
void QThread::exit(int returnCode = 0);
bool QThread::wait(unsigned long time = ULONG_MAX);
槽
[slot] void QThread::quit();
[slot] void QThread::start(Priority priority = InheritPriority);
[slot] void QThread::terminate();
信号
[signal] void QThread::finished();
[signal] void QThread::started();
主线程和子线程间通过信号和槽进行数据传递
静态函数
[static] QThread *QThread::currentThread();
[static] int QThread::idealThreadCount();
[static] void QThread::msleep(unsigned long msecs);
[static] void QThread::sleep(unsigned long secs);
[static] void QThread::usleep(unsigned long usecs);
任务处理函数
[virtual protected] void QThread::run();
该函数没有参数和返回值,是一个受保护的虚函数,如果让子线程处理某些任务,需要将任务写到run内部,调用start函数就能执行线程内部的run函数
受保护说明该函数不能在类外使用,需要通过调用start槽函数使run函数运行执行任务
最简单的Qt线程
------------------------------------------------
Widget.h
#include <QWidget>
#include "thread.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
Thread thread;
void handle();
};
------------------------------------------------------------
Widget,cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
thread.start();
connect(&thread,&Thread::notify,this,&Widget::handle);
}
Widget::~Widget()
{
delete ui;
}
void Widget::handle()
{
int val=ui->lcdNumber->intValue();
val--;
ui->lcdNumber->display(val);
}
--------------------------------------
Thread.h 继承自QThread
#include <QWidget>
#include <QThread>
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
void run();
signals:
void notify();
};
-------------------------------------------
#include "thread.h"
Thread::Thread()
{
}
void Thread::run()
{
for(int i=0;i<10;i++)
{
sleep(1);
emit notify();
}
}
使用方式
方式1:
- 创建线程类,该类继承QThread类
- 在线程的子类中重写run函数
- 在主线程类中new一个子线程的类对象,让该子线程对象调用start函数
子类头文件
#include <QThread>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
protected:
void run();
signals:
void curNumber(int num);
public slots:
};
子类源文件
#include "mythread.h"
#include <QDebug>
MyThread::MyThread(QObject *parent) : QThread(parent)
{}
void MyThread::run()
{
qDebug() << "当前线程对象的地址: " << QThread::currentThread();
int num = 0;
while(1)
{
emit curNumber(num++);
if(num == 10000000)
{
break;
}
QThread::usleep(1);
}
qDebug() << "run() 执行完毕, 子线程退出...";
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug() << "主线程对象地址: " << QThread::currentThread();
MyThread* subThread = new MyThread;
connect(subThread, &MyThread::curNumber, this, [=](int num)
{
ui->label->setNum(num);
});
connect(ui->startBtn, &QPushButton::clicked, this, [=]()
{
subThread->start();
});
}
MainWindow::~MainWindow()
{
delete ui;
}