Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

c++ - 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!
";
}

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 ..estQtProjectmain.cpp:15
question from:https://stackoverflow.com/questions/65617293/qt-slot-and-signal-are-not-connected-no-such-signal

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...