How do you connect the slot of the view with the signal of the class used?
-
Hello, everyone, there's a view of MainWindow and a class finder. How do you connect the slot of the MainWindow view with the class finder signal? In the classroom, finder signaled signals: void redraw();
In MainWindow, he announced that he had a class variable.
#include "mainwindow.h" #include "QtWidgets" #include <Qpainter> #include <QDebug> #include <finder.h>
finder robot;
Next, in MainWindow, trying to connect the class finder signal and the MainWindow slot as follows:
connect(& robot, SIGNAL(redraw()), this, SLOT(update()));
Can you tell me how this problem can be solved?
-
I'll give you a minimum compilable example of how to deal with scattered 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"