Solved 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. -
@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 -
void classA::onClicked() { classB myClassB; //created other class instance myClassB.BUTTON_EVENT(); //called its function }
-
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.