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);
    

    In the class constructor:

        Object::connect(this, SIGNAL(clicked(bool)), this, SLOT(clicked(bool)));
    

    Everything builds without warnings or errors, when I execute the code I get the following in the Application Output pane:

         QObject::connect: No such slot QPushButton::clicked(bool)

  • Moderators

    @SPlatten
    naming 2 functions the same, with the same arguments. That's bound to cause problems.

    The only way you got away with it in the first place is, because you derived the class containing one function.

    But, I'm pretty sure the mommoc-compiler is running into issues here because of it.



  • That isn't the problem, I just renamed the slot to "clickedHandler" exactly the same problem, no change.



  • @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


  • Qt Champions 2018

    @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 be

    QObject::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;
    }
    

  • Qt Champions 2018

    @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.

    https://wiki.qt.io/New_Signal_Slot_Syntax#Overload


  • Moderators

    @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)));
             } 
         }

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.