创建子线程working函数
lass xiancheng : public QObject
{
Q_OBJECT
public:
explicit xiancheng(QObject *parent = nullptr);
void working() {
qDebug()<<"子线程工作了";
qDebug()<<"当前子线程地址:"<<QThread::currentThread();
int m = 10;
};
}
方式一 点击按钮触发子线程,方式二 程序启动自动触发子线程
//测试线程
QToolButton * btn = new QToolButton(this);
btn->setIcon(QIcon(":/res/FZ.png"));
connect(btn,&QToolButton::clicked,this,[=](){
qDebug()<<"当前主程地址:"<<QThread::currentThread();
});
QThread *ti = new QThread(this);
xiancheng *xian = new xiancheng;//不能指定父对象
xian->moveToThread(ti);
//connect(btn,&QToolButton::clicked,xian,&xiancheng::working);//点击按钮触发子线程
connect(ti,&QThread::started,xian,&xiancheng::working);//程序启动自动触发子线程
ti->start();
connect(this,&w1::destroyed,this,[=](){
ti->quit();
ti->wait();
xian->deleteLater();
// delete xian;
qDebug()<<"子线程销毁";
});