常量 | 描述 | |
---|---|---|
Qt::AutoConnection | (默認)若是接收方位於發出信號的線程中,則使用Qt::DirectConnection。不然,使用Qt::QueuedConnection。鏈接類型在信號發出時肯定。 | |
Qt::DirectConnection | 當發出信號時當即調用插槽。插槽在信號線程中執行。 | |
Qt::QueuedConnection | 當控件返回到接收方線程的事件循環時調用插槽。插槽在接收器的線程中執行 | |
Qt::BlockingQueuedConnection | 與Qt::QueuedConnection相同,只是發出信號的線程阻塞,直到插槽返回。若是接收方位於發出信號的線程中,則不能使用此鏈接,不然應用程序將死鎖。 | |
Qt::UniqueConnection | 這是一個能夠使用按位OR與上述任何一種鏈接類型組合的標誌。 當設置Qt :: UniqueConnection時,若是鏈接已經存在,QObject :: connect()將失敗(即,若是相同的信號已鏈接到同一對對象的相同插槽)。 這個標誌在Qt 4.6中引入。 |
在 多線程中發射信號 ,在主線程中採用 lambda 表達式接收該信號。多線程
DirectConnection
時,lambda 在多線程中運行。QueuedConnection
時,lambda 在主線程中運行。BlockingQueuedConnection
時,lambda 在主線程中運行。Qt::UniqueConnection
標誌能夠用來方式重複鏈接同一個信號和槽。函數
瞭解以上信號和槽的鏈接方式之後,能夠用這個函數原型來更改接收方執行函數的線程this
[static] QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection)
QObject::connect(this,&Widget::newData,this,[&](QString text){ qDebug() << "slot thread ID:" << QThread::currentThreadId(); QMessageBox::information(this,"test",text); },Qt::QueuedConnection);