Hello everyone, there is a MainWindow widget and a class finder. How can I associate the MainWindow widget slot with a finder class signal? In the class finder, it declared signals: void redraw ();

In MainWindow declared created a variable with a class

#include "mainwindow.h" #include "QtWidgets" #include <Qpainter> #include <QDebug> #include <finder.h> finder robot; 

Next, in the MainWindow constructor, I try to connect the finder class signal and the slot from the MainWindow as follows:

 connect(& robot, SIGNAL(redraw()), this, SLOT(update())); 

Tell me, how can I solve this problem?

  • By the code that is presented now, everything looks fine. Show the compiler error message. - aleks.andr
  • @ aleks.andr C: \ Qt \ LabrynthWidget \ Labrinth \ mainwindow.cpp: 18: error: no matching function for 'MainWindow :: connect (finder *, const char *, MainWindow *, const char *)' connect ( & robot, SIGNAL (redraw ()), this, SLOT (update ())); 18 line - connect (& robot, SIGNAL (redraw ()), this, SLOT (update ())); Maybe I missed one moment, the class finder is not Q_OBJECT (it’s possible to connect signals and slots only for classes inherited from QOjbect?) - Malice
  • one
    You yourself answered your question: you can connect signals and slots only in classes inherited from QObject . - aleks.andr
  • @ aleks.andr In the class header file, the finder added: #include <QObject> 9. class finder: public QObject 10. {11. Q_OBJECT Now the compiler produces the following error: C: \ Qt \ LabrynthWidget \ Labrinth \ finder.h: 9: error: undefined reference to `vtable for finder ' - Malice
  • Down with a global instance of a class. - gbg

1 answer 1

I will give the minimum compiled example of working with slotted signals:

mobject.h

 #include <QObject> class QString; class MObject : public QObject { Q_OBJECT public: explicit MObject(const QString &objName, QObject *parent = 0); void emitSignal(); signals: void testSignal(); public slots: void testSlot(); }; 

mobject.cpp:

 #include <QDebug> MObject::MObject(const QString &objName, QObject *parent) : QObject(parent) { setObjectName(objName); } void MObject::emitSignal() { emit testSignal(); } void MObject::testSlot() { qDebug() << QString("%1::testSlot() call by %2 ") .arg(this->objectName()) .arg(sender()->objectName()); } 

main.cpp:

 #include <QCoreApplication> #include "mobject.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MObject objA("objA"); MObject objB("objB"); QObject::connect(&objA, &MObject::testSignal, &objB, &MObject::testSlot); QObject::connect(&objB, &MObject::testSignal, &objA, &MObject::testSlot); objA.emitSignal(); objB.emitSignal(); return a.exec(); } 

Result:

 "objB::testSlot() call by objA" "objA::testSlot() call by objB"