Qt 5 Signal/Slot 連接方法技術細節分享:
Qt 自 5.0 提供了兩種編寫Signal/Slot連接的方法:基於字串的連接語法(需搭配 moc + SIGNAL/SLOT
)和基於functor-based的連接語法(使用PointerToMemberFunction)。
先列出 Qt 4以前的Signal/Slot連接的語法(基於字串的連接語法,Qt4.8版本):
connect(const QObject *, const char *,
const QObject *, const char *, Qt::ConnectionType) : bool
--> 完整語法 bool QObject::connect(const QObject * sender, const char * signal,
const QObject * receiver, const char * method, Qt::ConnectionType type =
Qt::AutoConnection)
connect(const QObject *, const QMetaMethod
&, const QObject *, const QMetaMethod &, Qt::ConnectionType) : bool
connect(const QObject *, const char *,
const char *, Qt::ConnectionType) const : bool
disconnect(const QObject *, const char *,
const QObject *, const char *) : bool
disconnect(const QObject *, const
QMetaMethod &, const QObject *, const QMetaMethod &) : bool
disconnect(const char *, const QObject *,
const char *) : bool
disconnect(const QObject *, const char *) :
bool
以下是 Qt 5目前的Signal/Slot連接的語法, 在 connect 多三個,加了PointerToMemberFunction
及 Functor:
connect(const QObject *, const char *,
const QObject *, const char *, Qt::ConnectionType ) : QMetaObject::Connection
connect(const QObject *, const QMetaMethod
&, const QObject *, const QMetaMethod &, Qt::ConnectionType ) :
QMetaObject::Connection
connect(const QObject *, const char *,
const char *, Qt::ConnectionType ) const : QMetaObject::Connection
connect(const QObject *,
PointerToMemberFunction , const QObject *, PointerToMemberFunction , Qt::ConnectionType
) : QMetaObject::Connection
connect(const QObject *,
PointerToMemberFunction , Functor ) : QMetaObject::Connection
connect(const QObject *,
PointerToMemberFunction , const QObject *, Functor , Qt::ConnectionType ) :
QMetaObject::Connection
disconnect(const QObject *, const char *,
const QObject *, const char *) : bool
disconnect(const QObject *, const
QMetaMethod &, const QObject *, const QMetaMethod &) : bool
disconnect(const char *, const QObject *,
const char *) const : bool
disconnect(const QObject *, const char *)
const : bool
disconnect(const QMetaObject::Connection
&) : bool
disconnect(const QObject *,
PointerToMemberFunction , const QObject *, PointerToMemberFunction ) : bool
且 Qt 5繼續支持舊的基於字串的連接語法, 因這兩種連接語法各有優缺點, 簡述如下:
基於字串的連接語法
|
基於functor-based的連接語法
|
|
Type checking在何時完成
|
Run-time
|
編譯時
|
可以執行隱式類型轉換
|
Y
|
|
可以將信號連接到lambda表達式
|
Y
|
|
可以將信號連接到參數多於信號的插槽
|
Y
|
|
可以將C ++函數連接到QML函數
|
Y
|
沒有留言:
張貼留言