Solved How to pass signal from one class to another.
-
How to pass signal from one class to another.
This is the first class
#include <QtWidgets/QMainWindow> #include "ui_MouseClickTest.h" class MouseClickTest : public QMainWindow { Q_OBJECT public: MouseClickTest(QWidget *parent = Q_NULLPTR); protected: void mousePressEvent(QMouseEvent* event) override; signals: void MouseClicked(QMouseEvent* event); private: Ui::MouseClickTestClass ui; };
i want to pass the MouseClicked signal to another class , whenever any object of this class is created it should be connected to the signal from the MouseClickTest class.
class Geometry : public QObject { public slots: void Clicked(QMouseEvent* event); };
-
@summit Well, you connect the signal to the slot using connect(). What exactly is your question/problem?
https://doc.qt.io/qt-5/signalsandslots.html -
@jsulm What would be the connect syntax in this case where i have not created any objects , i can connect a object of geometry to MouseClickTest class but how can i do it without creating any object.
-
@summit said in How to pass signal from one class to another.:
without creating any object.
You can't. Signals & slots both require an object instance to
connect()
. You have to go to wherever the second one --- usually the slot object --- is created, or the caller of where it is created, and do your connection there. For each slot object instance you create. -
@JonB Is it possible to do it in any other way , or assign mouseclick() event to Geometry class ?
-
@summit
No. That's why I wrote "You can't", else I would have said "You can". Look at the definitions of the variousconnect()
overloads.No matter how many times you ask, the answer still remains the connections are between object instances of the signaller and the slotter.
-
@JonB Thanks just one more question can i use this function in Geometry Class.
void mousePressEvent(QMouseEvent* event) override; -
@summit said in How to pass signal from one class to another.:
can i use this function in Geometry Class
No, since it is not a QWidget
-
@summit
By the look of it: make yourvoid MouseClickTest::mousePressEvent(QMouseEvent* event) override
goemit MouseClicked(event)
. Now you have a signal with the desired parameter, which you canconnect()
to eachGeometry* geometryInstance
's&Geometry::Clicked
slot. -
@JonB Thanks very much for the answer.
-
@JonB said in How to pass signal from one class to another.:
You can't. Signals & slots both require an object instance to connect(
Thats not strictly correct, you can connect without an receiver QObject, the overload exists for a reason. but the sender has to be an QObject
https://doc.qt.io/qt-5/qobject.html#connect-2
https://doc.qt.io/qt-5/qobject.html#connect-4That said, @summit what actually do you want to archive with the MouseClick event? maybe you're asking the wrong question here
-
@J-Hilk said in How to pass signal from one class to another.:
you can connect without an receiver QObject
You can indeed, but you cannot use that to connect to instances of the OP's
Geometry
, which is what he is trying to do. He might write, e.g., astatic
method inGeometry
to use as the slot, but it won't then do what he has in mind, which is to "auto-connect" each instance created to main window's signal.