`&ClassName::function` 调用方法代表什么?
What does `&ClassName::function` method of calling represent?
connect( &objTwo, &Two::emitThisSignal, this, &Controller::mySlot );
这里的二是分开的class。它的对象已在 Controller class 中创建。它的信号必须连接到控制器 class 的插槽。
连接的签名是:
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
我的连接调用代表上面的哪个签名?
为什么我不能简单地写:connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );
?
Which signature out of above does my connect call represent?
这个:
connect(const QObject *sender, PointerToMemberFunction signal,
const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
最后一个参数是默认值(Qt::ConnectionType type = Qt::AutoConnection
),因此您没有明确指定它。 Link to docs
Why can't I simply write: connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );?
因为如果您想将指针传递给成员函数,那是无效的 C++ 语法。 objTwo.emitThisSignal
在 C++ 语法中意味着,访问 objTwo
中的 data 成员 emitThisSignal
,而函数需要指向成员函数的指针。
但是如果你写:
connect( &objTwo, SIGNAL(objTwo.emitThisSignal), this, SLOT(this->mySlot) );
您的代码可能会编译但无法按预期工作。看第一个 connect
签名就可以理解其中的原因:
connect(const QObject *sender, const char *signal,
const QObject *receiver, const char *method, Qt::ConnectionType type)
它将指向 char*
的指针作为第二个和第四个参数。所以任何语法都会被编译。宏 SIGNAL()
和 SLOT()
将它们转换为字符串,然后 Qt 在运行时使用它们在发出信号时调用正确的槽。上述语法甚至可能不会导致崩溃或任何警告。我知道这听起来很可怕,但这就是 Qt 5 出现之前 Qt 的状态。
最近强烈推荐您使用这三个签名:
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
connect( &objTwo, &Two::emitThisSignal, this, &Controller::mySlot );
这里的二是分开的class。它的对象已在 Controller class 中创建。它的信号必须连接到控制器 class 的插槽。
连接的签名是:
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
我的连接调用代表上面的哪个签名?
为什么我不能简单地写:connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );
?
Which signature out of above does my connect call represent?
这个:
connect(const QObject *sender, PointerToMemberFunction signal,
const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
最后一个参数是默认值(Qt::ConnectionType type = Qt::AutoConnection
),因此您没有明确指定它。 Link to docs
Why can't I simply write:
connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );?
因为如果您想将指针传递给成员函数,那是无效的 C++ 语法。 objTwo.emitThisSignal
在 C++ 语法中意味着,访问 objTwo
中的 data 成员 emitThisSignal
,而函数需要指向成员函数的指针。
但是如果你写:
connect( &objTwo, SIGNAL(objTwo.emitThisSignal), this, SLOT(this->mySlot) );
您的代码可能会编译但无法按预期工作。看第一个 connect
签名就可以理解其中的原因:
connect(const QObject *sender, const char *signal,
const QObject *receiver, const char *method, Qt::ConnectionType type)
它将指向 char*
的指针作为第二个和第四个参数。所以任何语法都会被编译。宏 SIGNAL()
和 SLOT()
将它们转换为字符串,然后 Qt 在运行时使用它们在发出信号时调用正确的槽。上述语法甚至可能不会导致崩溃或任何警告。我知道这听起来很可怕,但这就是 Qt 5 出现之前 Qt 的状态。
最近强烈推荐您使用这三个签名:
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)