German
German

Reputation: 345

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

Upvotes: 0

Views: 59

Answers (1)

kzsnyk
kzsnyk

Reputation: 2211

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

Upvotes: 2

Related Questions