简体   繁体   中英

Qt Slot and signal are not connected: No such signal

I am trying to deal with slots and signals in Qt, for this I am trying to do the following:
The MyTestClass class should send a signal to the ReceiverClass class, the code:

mytestclass.h

class MyTestClass : public QObject
{
    Q_OBJECT
public:
    MyTestClass();
    void makeSignal();
signals:
    void sendSignal();
};

mytestclass.cpp

MyTestClass::MyTestClass()
{
}

void MyTestClass::makeSignal()
{
    emit sendSignal();
}

reseiverclass.h

class ReceiverClass : public QObject
{
    Q_OBJECT
public:
    ReceiverClass();
public slots:
    void receiverSlot();
};

reseiverclass.cpp

ReceiverClass::ReceiverClass()
{
}

void ReceiverClass::receiverSlot()
{
    qInfo() << "receiverSlot called!\n";
}

main.cpp

...
MyTestClass testObj;
ReceiverClass receiverObj;

QObject::connect(&testObj, SIGNAL(&testObj::sendSignal()), &receiverObj, SLOT(&receiverObj::receiverSlot));

testObj.makeSignal();
...

However, I encounter such an error.
Why doesn't Qt see the signal?

QObject::connect: No such signal MyTestClass::&testObj::sendSignal() in ..\testQtProject\main.cpp:15
QObject::connect(&testObj, SIGNAL(&testObj::sendSignal()), &receiverObj, SLOT(&receiverObj::receiverSlot));

You are mixing the syntax of the 2 methods for connecting signal and slots in Qt, either use:

QObject::connect(&testObj, SIGNAL(sendSignal()), &receiverObj, SLOT(receiverSlot()));

or

QObject::connect(&testObj, &testObj::sendSignal, &receiverObj, &receiverObj::receiverSlot);

For more informations, look at https://doc.qt.io/qt-5/signalsandslots.html and https://wiki.qt.io/New_Signal_Slot_Syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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