关于 QT(C++)通过继承 QObject 的方法实现多线程,子线程调用失效。

16次阅读

共计 55 个字符,预计需要花费 1 分钟才能阅读完成。

class MyThread : public QObject{
	Q_OBJECT
	void work(){qDebug()<<"MyThread::work() thread id:"<moveToThread(thread1);
    my_thread->start();	
    
    qDebug()<<"00000000";
    my_thread->work();
    
    connect(ui->pushButton, &QPushButton::clicked, my_thread, &MyThread::work);
    connect(ui->pushButton, &QPushButton::clicked, this, [=]{qDebug()<<"11111111";
    	my_thread->work();});
    connect(ui->pushButton, &QPushButton::clicked, my_thread, [=]{qDebug()<<"22222222";
        my_thread->work();});
}

程序执行结果:

main thread id:  0x1a70 

00000000
MyThread::work1() thread id:  0x1a70 

11111111
MyThread::work1() thread id:  0x1a70 

MyThread::work1() thread id:  0x698 

22222222
MyThread::work1() thread id:  0x698 

请问为何在主窗口中直接调用 my_thread 和
connect(ui->pushButton, &QPushButton::clicked, this, [=]{
my_thread->work();
}); 都是在主线程中执行,而其他两种调用方式就能正确在子线程中执行?

正文完
 0