Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal / Socket connection problem...
Forum Updated to NodeBB v4.3 + New Features

Signal / Socket connection problem...

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS SPlatten

    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)
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #5

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

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    3
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #6

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

      Kind Regards,
      Sy

      jsulmJ Pradeep P NP KroMignonK 3 Replies Last reply
      0
      • Pradeep P NP Offline
        Pradeep P NP Offline
        Pradeep P N
        wrote on last edited by Pradeep P N
        #7

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

        Pradeep Nimbalkar.
        Upvote the answer(s) that helped you to solve the issue...
        Keep code clean.

        1 Reply Last reply
        1
        • SPlattenS SPlatten

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #8

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • SPlattenS SPlatten

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

            Pradeep P NP Offline
            Pradeep P NP Offline
            Pradeep P N
            wrote on last edited by Pradeep P N
            #9

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

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            1 Reply Last reply
            1
            • SPlattenS SPlatten

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

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #10

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

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #11

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

                Kind Regards,
                Sy

                KroMignonK J.HilkJ 2 Replies Last reply
                1
                • SPlattenS SPlatten

                  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);
                      };
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #12

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

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  0
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #13

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

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • Pradeep P NP Offline
                      Pradeep P NP Offline
                      Pradeep P N
                      wrote on last edited by
                      #14

                      @SPlatten
                      Also here is the reason why new connect syntax donot work in this case.

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

                      Pradeep Nimbalkar.
                      Upvote the answer(s) that helped you to solve the issue...
                      Keep code clean.

                      1 Reply Last reply
                      1
                      • SPlattenS SPlatten

                        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);
                            };
                        
                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by J.Hilk
                        #15

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


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        0
                        • SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by SPlatten
                          #16

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

                          Kind Regards,
                          Sy

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved