Solved Signal / Socket connection problem...
-
@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?
-
@SPlatten Please check with
connect(this, SIGNAL(clicked(bool)), this, SLOT(clicked(bool)));
It works.
There is no issue in Build but still the Slot is not called when using new connect syntax.
-
@SPlatten said in Signal / Socket connection problem...:
still don't see anything in the slot which I've now renamed to "clickedHandler"
What do you mean? Is the slot not called? Are you sure you are watching/click on the right button?
-
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!
-
As I said, I thought wrongly that since my class was derived from one with that functionality already present, that my new class would inherit the same, live and learn :)
-
@SPlatten
Also here is the reason why new connect syntax donot work in this case. -
@SPlatten
also in case you didn't do it. Initializing the "parent QObject-class" in the constructor is a good practice you should always do. -
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))); } }