How to pass a slot as a parameter to another class
-
I'm new one to Qt, I have been using the C language callback function, and now I want to try to reimplement it with the signal slot mechanism.
There is a button in ClassA, and ClassB will Instantiate an ClassA object.
I need classB implement the ClassA's button event, and send the SLOT to ClassA.
Then I can connect the SLOT with button in ClassA's Constructor.//classA.h class classA { public: classA(QWidget *parent = 0, BUTTON_CLICKED_FUNCTION); private: QPushButton *pushButton; } //classA.cpp #include "classA.h" classA::classA(QWidget *parent, BUTTON_CLICKED_FUNCTION) { connect(pushButton, SIGNAL(clicked()), this, SLOT(BUTTON_CLICKED_FUNCTION)); } //classB.h class classB{ public: classB(); public slots: void BUTTON_EVENT(); } //classB.cpp #include "classB.h" #include "classA.h" classB::classB() { classA *object = new classA(this, BUTTON_EVENT); } classB::BUTTON_EVENT() { //do something... }
Is there any way to achieve it or achieve something similar to this?
Thank you guys.
-
@Astralooo
since you are using the old connect syntax you can simply pass a char* as the parameter type. You then would have to call the function/constructor with the SLOT() macro -
Since you will use a function from classB in classA, you should import the classB into classA first. Then define a new "subclass" as the type classB inside classA. Then, you can use the function.
classA.hclass classA { public: explicit classA(QWidget *parent = nullptr); public slots: void onClicked(); //added to call classB function private: QPushButton *pushButton; }
classA.cpp
#include "classB.h" #include "classA.h" classA::classA(QWidget *parent) { connect(pushButton, SIGNAL(clicked()), this, SLOT(onClicked())); } void classA::onClicked() { classB *myClassB = new classB; //created subclass myClassB->BUTTON_EVENT(); //called its function }
classB.h
class classB{ public: classB(); public slots: void BUTTON_EVENT(); }
classB.cpp
#include "classB.h" //dont need to include classA.h classB::classB() { } void classB::BUTTON_EVENT() { //do something... }
-
@closx
I don't claim to understand what's going on here, because the OP wrote:ClassB will Instantiate an ClassA object
while you have:
void classA::onClicked() { classB *myClassB = new classB; //created subclass myClassB->BUTTON_EVENT(); //called its function }
which creates
classB
insideclassA
and is the opposite of what the OP has written.I don't get your whole approach, but anyway if you are going to put the above code in for others to read: as it stands it leaks, which I think you should address....
-
@JonB Yeah you are kinda right actually. But my approach is showing a easy way to call a function from another class by clicking a button. If OP wants to receive an classA object from classB, then they can do it :D My code was not something like "change your code like this". It was more like "if you do that, function would be called. You can inspire from that".
-
@closx
That's as may be, but does not address the leak in your example code which others may be encouraged to follow.For the sake of a one line change, why not
delete classB
orclassB.deleteLater()
depending, or simpler still allocate on stack instead of heap? I've said my piece. -
@Astralooo As you are already using Qt and QObject derived classes, you can simply add a "buttonClicked" signal in you class A
//classA.h class classA : public QWidget{ //... signals: void buttonClicked(); //... }; //classA.cpp classA::classA(QWidget *parent) : QWidget(parent) { connect(pushButton, &QPushButton::clicked, this, &classA::buttonClicked); } //classB.h class classB : public QWidget{ //... private slots: void aClicked(); //... }; //classB.cpp: classB::classB() : QWidget(parent) { classA *object = new classA(this); connect(object, &classA::buttonClicked, this, &classB::aClicked()) } void classB::aClicked(){ //Button clicked slot !! }
-
@JonB said in How to pass a slot as a parameter to another class:
@closx
That's as may be, but does not address the leak in your example code which others may be encouraged to follow.For the sake of a one line change, why not
delete classB
orclassB.deleteLater()
depending, or simpler still allocate on stack instead of heap? I've said my piece.Dude, you are really pushing my knowledge of English so hard. I am just tryna learn qt and make someone learn easier if I can. If there are mistakes I make, just show me and others.
Be positive, have a lovely day :D -
Hello!
Also, I would like to add that you can delete it when dialog exists using
setAttribute(Qt::WA_DeleteOnClose);
method and set theparent
to avoid any memory leaks. You can read about it here: https://doc.qt.io/archives/qt-4.8/objecttrees.html -
@closx @JonB @Gojir4 @Cobra91151 Thank you for the reply.
I'm sorry I didn't describe it clearly.ClassA may be called by different classes, and I'm not sure which Class will call the ClassA.
Each class that calls ClassA has a different slot method to do different things. So that's why I want these classes can pass a slot. What I want do is you pass me a slot or a function, I connect that slot or function with my button.
Sorry again about the unclear description , and thank you all.
-
@Astralooo said in How to pass a slot as a parameter to another class:
I want these classes can pass a slot
Don't do that. You're doing it wrong way.
As @Gojir4 suggested add a signal to your ClassA and then all the other classes can connect their slots to that signal and ClassA does not have to care about any slots in any other classes. This is how signals/slots are actually used. -
@jsulm
I have seen this way on other place. It's also the wrong way right?//classA.h class classA : public QWidget{ public: ClassA (QWidget *parent = nullptr, const char * buttonSlot = nullptr); public slots: void buttonClicked(); private: const char * slotName; //... }; //classA.cpp classA::classA(QWidget *parent, const char * buttonSlot) : QWidget(parent) { connect(pushButton, &QPushButton::clicked, this, &classA::buttonClicked); slotName = buttonSlot; } classA::buttonClicked() { QMetaObject::invokeMethod(parent(), slotName); } //classB.h class classB : public QWidget{ //... private slots: void aClicked(); //... }; //classB.cpp: classB::classB() : QWidget(parent) { classA *object = new classA(this, SLOT(aClicked())); } void classB::aClicked(){ //Button clicked slot !! }
-
@Astralooo I don't see where you're using slotName?
You can do this this way, but WHY?
What's the point?
Why not simply provide a signal, so all interested classes can connect their slots? -
slotName is used as a parameter for QMetaObject::invokeMethod(), so the invokeMethod function can run the SLOT function.
The current team's requirement for me is : when instantiate an object of my class, pass me in a callback, so they don't need to do anything more, such as connect. This is the reason I asked this question.
After you reply, I think I should discuss it with the team. I think the signal way is better.
Thanks a lot. -
@Astralooo said in How to pass a slot as a parameter to another class:
After you reply, I think I should discuss it with the team
Agree. If there is some special reason to do it this way then its OK, else you should stick to normal signal/slot approach.