Signal / Socket connection problem...
-
@SPlatten said in Signal / Socket connection problem...:
I have written a class which is derived from QPushButton, in my derived class I want to connect the "clicked" signal to a slot in the same class also called "clicked". In my derived class:
private slots:
void clicked(bool blnChecked);I don't think it is a good idea to have a signal and a slot have same name!
I would remane slot to
private slots: void clicked(bool blnChecked);
And use new connection syntax to got connection failures at compilation time and not runtime:
connect(this, &MyButton:clicked, this, &MyButton:onClicked);
And don't forget to add "Q_OBJECT":
class MyButton : QPushButton { Q_OBJECT ... private slots: void clicked(bool blnChecked); }
And perhaps also rerun qmake
Regards
-
@SPlatten said in Signal / Socket connection problem...:
QObject::connect: No such slot QPushButton::clicked(bool)
Are you sure this warning comes from the connect you pasted here?
Because the warning should beQObject::connect: No such slot YOURCLASSNAME::clicked(bool)
Are you trying to connect somewhere else also?
And is the warning now exact the same or does it contain the new slot name? -
@KroMignon, thank you, adding Q_OBJECT to my derived class has stopped the warning from being displayed, however when I click the button I still don't see anything in the slot which I've now renamed to "clickedHandler"
@jsulm, yes, positive, its the only connect I have.
-
Hi @SPlatten
Even using
private slots: void clicked(bool);
Works fine for me.
Below is my code
#include <QPushButton> class Widget : public QPushButton { Q_OBJECT public: Widget(QPushButton *parent = nullptr); ~Widget(); private slots: void clicked(bool); };
Widget::Widget(QPushButton *parent) : QPushButton(parent) { // New syntax // connect(this, &Widget::clicked, this, &Widget::clicked); // Old syntax connect(this, SIGNAL(clicked(bool)), this, SLOT(clicked(bool))); } void Widget::clicked(bool) { qDebug() << Q_FUNC_INFO << endl; }
-
@SPlatten said in Signal / Socket connection problem...:
I still don't see anything in the slot
What do you mean by that? What are you doing in the slot?
-
-
Ok, I've now got it working and just for the record and anyone else needing do so the same, this is what I needed to do. I thought 'wrongly' that because my class was derived from QPushButton which already has Q_OBJECT in it, that I didn't need to do the same, wrong!
Adding Q_OBJECT to the class solved the problem. Also having a signal and slot with the same name does not cause a problem, it's quite possible to have both with the exact same name.
My class, still a work in progress but working:
class clsMenuBtn; typedef List<clsMenuBtn*> lstOfMenus; class clsMenuBtn : public QPushButton { Q_OBJECT private: bool mblnSecure; clsMenuBtn* mpChildren, *mpNext, *mpParent, *mpPrev; public slots: void clicked(bool blnChecked); public: clsMenuBtn(QString strText = "", QWidget* pobjParent = NULL ,bool blnSecure = false); lstOfMenus lstGetMenus(); clsMenuBtn* pAddOption(QString strText = "", bool blnSecure = false); clsMenuBtn* pGetChildren() { return mpChildren; } clsMenuBtn* pGetNext() { return mpNext; } clsMenuBtn* pGetParent() { return mpParent; } clsMenuBtn* pGetPrev() { return mpPrev; } void setText(const QString& strText); };
-
@SPlatten said in Signal / Socket connection problem...:
Adding Q_OBJECT to the class solved the problem
When you are creating a class which is based on QObject and you want to add new signals and/or slots, you have to add Q_OBJECT macro to enable "MOC magic". That is mandatory!
-
@SPlatten
Also here is the reason why new connect syntax donot work in this case. -
I do, my code for the implementation of the constructor:
/** * @brief clsMenuBtn::clsMenuBtn * @param strText Optional, text for menu * @param pobjParent Optional, pointer to parent object * @param blnSecure Optional, secure flag default is false */ clsMenuBtn::clsMenuBtn(QString strText, QWidget* pobjParent, bool blnSecure) : QPushButton(pobjParent) { mblnSecure = blnSecure; mpChildren = mpNext = mpParent = mpPrev = NULL; setText(strText); if ( strText.isEmpty() != true ) { QObject::connect(this, SIGNAL(clicked(bool)), this, SLOT(clicked(bool))); } }