跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

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

发表于
class MyThread : public QObject{
	Q_OBJECT
	void work(){	 
    	qDebug()<<"MyThread::work() thread id: "<<QThread::currentThreadId()<<"\n";	
    }
}
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    qDebug()<<"main thread id: "<<QThread::currentThreadId()<<"\n";
    QThread* subThread = new QThread;
    MyThread* my_thread = new MyThread;
    my_thread->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(); });都是在主线程中执行,而其他两种调用方式就能正确在子线程中执行?

Featured Replies

没有可显示的帖子

创建帐户或登录来提出意见

Account

导航

搜索

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.