表現方式 Qt中Signal Slot 4 種連接
表現方式 Qt 中Signal Slot 4 種連接是本文介紹的內容,先來看內容。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需要復制到隊列里)。
與wxWidget、MFC的效率比較
wxWidget沒用過,了解過MFC。我覺得MFC的消息隊列是比較簡單的,一般只是傳遞long數值、HANDLE、指針,效率上應該會比QT的隊列方式高,然而QT的靈活性是MFC沒辦法比擬的,我還是比較喜歡QT這樣的。
小結:表現方式 Qt 中Signal Slot 4 種連接的內容介紹完了,希望本文對你有所幫助。