how to connect a signal from one class to a slot in another class ?
-
@Prakash08 your code looks ok, although you seem to have mixed code from header file and source file.
You can simplify this by connecting the signal from push button directly to your model:
connect(ui->pushButton, &QPushButton::clicked, &mod, &Model::came_here);
So, to sum up. This should be in header file (.h):
signals: void eject(); Model mod;
And this in source file (.cpp), preferably somewhere early like in the constructor of your main window:
connect(ui->pushButton, &QPushButton::clicked, &mod, &Model::came_here);
-
@Prakash08 said in how to connect a signal from one class to a slot in another class ?:
but if i want to connect a custom signal to slot from other class is that possible ?
Of course it is possible, why should it not be possible?
It doesn't matter whether the signal is your own custom signal... -
@Prakash08 let me chatgpt that for you:
To connect a custom signal from one class to a slot in another class, you can follow a similar approach as you did with the built-in signals and slots. Here's how you can do it:
In the header file of the class containing the custom signal (let's call it
SenderClass
), declare the custom signal:class SenderClass : public QObject { Q_OBJECT signals: void customSignal(); };
In the header file of the class containing the slot (let's call it
ReceiverClass
), declare the slot:class ReceiverClass : public QObject { Q_OBJECT public slots: void customSlot(); };
Then, in the implementation file of the
ReceiverClass
, define the custom slot:#include "receiverclass.h" // Include the header file of ReceiverClass void ReceiverClass::customSlot() { qDebug() << "Custom slot executed"; }
Finally, connect the custom signal to the slot in your code:
SenderClass sender; ReceiverClass receiver; // Connect the custom signal from SenderClass to the custom slot in ReceiverClass QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot); // Emit the custom signal from SenderClass emit sender.customSignal();
With this setup, when the custom signal
customSignal()
is emitted from an instance ofSenderClass
, the slotcustomSlot()
in an instance ofReceiverClass
will be executed. Make sure to include the necessary header files and namespaces in your actual implementation. -
@Prakash08 said in how to connect a signal from one class to a slot in another class ?:
And how to code that ?
In exact same way.
What is the problem you face? -
@J-Hilk said in how to connect a signal from one class to a slot in another class ?:
SenderClass sender;
ReceiverClass receiver;// Connect the custom signal from SenderClass to the custom slot in ReceiverClass
QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot);// Emit the custom signal from SenderClass
emit sender.customSignal();in which file these lines to be written ?
-
@Prakash08 Emit should be clear, right? You emit a signal where you want/need to emit it. Usually sender class is emitting its signal.
First two lines: whereever you need these classes. connect() - put it where you have access to both, sender and receiver. -
@Prakash08 my friend you need to pick up some courses on c++ and qt this is as basic as it gets
// SenderClass.h #ifndef SENDERCLASS_H #define SENDERCLASS_H #include <QObject> class SenderClass : public QObject { Q_OBJECT public: explicit SenderClass(QObject *parent = nullptr); signals: void customSignal(); }; #endif // SENDERCLASS_H
// SenderClass.cpp #include "SenderClass.h" SenderClass::SenderClass(QObject *parent) : QObject(parent) { }
// ReceiverClass.h #ifndef RECEIVERCLASS_H #define RECEIVERCLASS_H #include <QObject> #include <QDebug> class ReceiverClass : public QObject { Q_OBJECT public: explicit ReceiverClass(QObject *parent = nullptr); public slots: void customSlot(); }; #endif // RECEIVERCLASS_H
// ReceiverClass.cpp #include "ReceiverClass.h" ReceiverClass::ReceiverClass(QObject *parent) : QObject(parent) { } void ReceiverClass::customSlot() { qDebug() << "Custom slot executed"; }
// main.cpp #include <QCoreApplication> #include "SenderClass.h" #include "ReceiverClass.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); SenderClass sender; ReceiverClass receiver; // Connect the custom signal from SenderClass to the custom slot in ReceiverClass QObject::connect(&sender, &SenderClass::customSignal, &receiver, &ReceiverClass::customSlot); // Emit the custom signal from SenderClass emit sender.customSignal(); return a.exec(); }