简体   繁体   English

`&ClassName::function` 的调用方法代表什么?

[英]What does `&ClassName::function` method of calling represent?

connect( &objTwo, &Two::emitThisSignal, this, &Controller::mySlot );

Here Two is a separate class.这里的二是一个单独的 class。 Its object has been created in Controller class.其 object 已在 Controller class 中创建。 Its signal has to be connected to Controller class's slot.它的信号必须连接到 Controller 类的插槽。

Signature of connect is:连接的签名是:

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)

Which signature out of above does my connect call represent?我的连接调用代表上面的哪个签名?

Why can't I simply write: connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );为什么我不能简单地写: connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot ); ? ?

Which signature out of above does my connect call represent?我的连接调用代表上面的哪个签名?

This one:这个:

connect(const QObject *sender, PointerToMemberFunction signal, 
       const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)

The last argument is default( Qt::ConnectionType type = Qt::AutoConnection ), so you don't have explicitly specify it.最后一个参数是默认的( Qt::ConnectionType type = Qt::AutoConnection ),所以你没有明确指定它。 Link to docs链接到文档

Why can't I simply write: connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );?为什么我不能简单地写: connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );?

Because that is invalid C++ syntax if you want to pass a pointer to member function.因为如果你想传递一个指向成员 function 的指针,那是无效的 C++ 语法。 objTwo.emitThisSignal in C++ syntax would mean, access the data member emitThisSignal inside objTwo , while the function expects a pointer to member function. objTwo.emitThisSignal语法中的 objTwo.emitThisSignal 意味着,访问objTwo中的数据成员emitThisSignal ,而 function 需要一个指向成员 ZC1C425268E68385D1AB5074C1 的指针


However if you write:但是,如果你写:

connect( &objTwo, SIGNAL(objTwo.emitThisSignal), this, SLOT(this->mySlot) );

Your code will probably compile but would not work as intended.您的代码可能会编译,但不会按预期工作。 The reason for this can be understood by look at the first connect signature:其原因可以通过查看第一个connect签名来理解:

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

It takes pointer to char* as 2nd and 4th arguments.它将指向char*的指针作为第二个和第四个 arguments。 So any syntax would get compiled.所以任何语法都会被编译。 The macros SIGNAL() and SLOT() convert these into strings and then Qt uses these at runtime to call the right slot(s) when a signal is emitted.SIGNAL()SLOT()将它们转换为字符串,然后 Qt 在运行时使用它们在发出信号时调用正确的插槽。 The above syntax will likely not even result in a crash or any warnings even.上述语法甚至可能不会导致崩溃或任何警告。 I know this sounds apalling, but such was the state of Qt before Qt 5 came.我知道这听起来很可怕,但在 Qt 5 出现之前,Qt 的 state 就是这样。

These days it is highly recommended that you use these three signatures:这些天来,强烈建议您使用这三个签名:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM