QT中SIGNAL與SLOT連接的幾種方式

signal/slot在底層會使用三種方式傳遞消息。參見QObject::connect()方法:

bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection )

最後一個參數是就是傳遞消息的方式了,有四個取值:


Qt::DirectConnection

When emitted, the signal is immediately delivered to the slot.

假設當前有4個slot連接到QPushButton::clicked(bool),當按鈕被按下時,QT就把這4個slot按連接的時間順序調用一遍。顯然這種方式不能跨線程(傳遞消息)。



Qt::QueuedConnection

When emitted, the signal is queued until the event loop is able to deliver it to the slot.

假設當前有4個slot連接到QPushButton::clicked(bool),當按鈕被按下時,QT就把這個signal包裝成一個 QEvent,放到消息隊列裏。QApplication::exec()或者線程的

QThread::exec()會從消息隊列裏取消息,然後調用 signal關聯的幾個slot。這種方式既可以在線程內傳遞消息,也可以跨線程傳遞消息。



Qt::BlockingQueuedConnection

Same as QueuedConnection, except that the current thread blocks until the slot has been delivered. This connection type should only be used for receivers in a different

thread. Note that misuse of this type can lead to dead locks in your application.

與Qt::QueuedConnection類似,但是會阻塞等到關聯的slot都被執行。這裏出現了阻塞這個詞,說明它是專門用來多線程間傳遞消息的。



Qt::AutoConnection

If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt::DirectConnection; otherwise the signal is queued, as with

Qt::QueuedConnection.

這種連接類型根據signal和slot是否在同一個線程裏自動選擇Qt::DirectConnection或Qt::QueuedConnection

這樣看來,第一種類型的效率肯定比第二種高,畢竟第二種方式需要將消息存儲到隊列,而且可能會涉及到大對象的複製(考慮sig_produced(BigObject bo),bo需要複製到隊

列裏)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章