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. How to connect with ButtonGroup clicked(AbstractButton) signal?

How to connect with ButtonGroup clicked(AbstractButton) signal?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 1.7k Views 2 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.
  • Tom assoT Tom asso

    Documentation says that ButtonGroup emits signal clicked(AbstractButton).

    My C++ code:

    QObject *obj =
        rootObject->findChild<QObject*>("groupName");
      
    QObject::connect((QObject *)obj, 
                    SIGNAL(clicked(QAbstractButton)),
                    &mainWindow, SLOT(onButtonClicked()));
    

    The code generates an error message:

    QObject::connect: No such signal ButtonGroup_QMLTYPE_127::clicked(QAbstractButton)
    

    What am I doing wrong?
    Thanks!

    C Offline
    C Offline
    ChrisW67
    wrote on last edited by
    #2

    The signal sends an AbstractButton not a QAbstractButton.

    Tom assoT 1 Reply Last reply
    0
    • C ChrisW67

      The signal sends an AbstractButton not a QAbstractButton.

      Tom assoT Offline
      Tom assoT Offline
      Tom asso
      wrote on last edited by Tom asso
      #3

      @ChrisW67 - I tried that but get similar error:

      QObject::connect((QObject *)obj, 
                      SIGNAL(clicked(AbstractButton)),
                      &mainWindow, SLOT(onButtonClicked()));
      
      QObject::connect: No such signal ButtonGroup_QMLTYPE_7::clicked(AbstractButton)
      
      B 1 Reply Last reply
      0
      • Tom assoT Tom asso

        @ChrisW67 - I tried that but get similar error:

        QObject::connect((QObject *)obj, 
                        SIGNAL(clicked(AbstractButton)),
                        &mainWindow, SLOT(onButtonClicked()));
        
        QObject::connect: No such signal ButtonGroup_QMLTYPE_7::clicked(AbstractButton)
        
        B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #4

        @Tom-asso What about SIGNAL(clicked(QAbstractButton*)), I mean normally this kind of signal has a pointer as param, not object.
        I don't quite know qml, this is just a guess.

        And there's a way to check the signals is

        obj->dumpObjectInfo();
        

        then look what's printed in the console.

        Tom assoT 1 Reply Last reply
        1
        • B Bonnie

          @Tom-asso What about SIGNAL(clicked(QAbstractButton*)), I mean normally this kind of signal has a pointer as param, not object.
          I don't quite know qml, this is just a guess.

          And there's a way to check the signals is

          obj->dumpObjectInfo();
          

          then look what's printed in the console.

          Tom assoT Offline
          Tom assoT Offline
          Tom asso
          wrote on last edited by Tom asso
          #5

          @Bonnie - ah, that is a useful function, but I don't understand the signals information it gives. Below is what it says for the ButtonGroup (my QML gives it objectName "editModesObj").
          Weird - no info on signal 'clicked()'! What is going on?

          OBJECT ButtonGroup_QMLTYPE_124::editModesObj
            SIGNALS OUT
                  signal: destroyed(QObject*)
                    --> QQuickColumnLayout::unnamed _q_resourceObjectDeleted(QObject*)
            SIGNALS IN
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                    <-- RadioButton_QMLTYPE_126::unnamed <unknown>
          

          ... and my C++ connects successfully to the ButtonGroup's destroyed() signal. My QML also declares a Slider, and my C++ can connect to its valueChanged() signal without problem. But dumpObjectInfo() for the slider does not mention the valueChanged() signal - but a signal "baselineOffsetChanged(double) - now I'm really confused!

          OBJECT Slider_QMLTYPE_7::pingStepSliderObj
            SIGNALS OUT
                  signal: baselineOffsetChanged(double)
                    <functor or function pointer>
                    --> QQuickColumnLayout::unnamed invalidateSenderItem()
            SIGNALS IN
                    <-- Slider_QMLTYPE_7::pingStepSliderObj <unknown>
          
          B 1 Reply Last reply
          0
          • Tom assoT Tom asso

            @Bonnie - ah, that is a useful function, but I don't understand the signals information it gives. Below is what it says for the ButtonGroup (my QML gives it objectName "editModesObj").
            Weird - no info on signal 'clicked()'! What is going on?

            OBJECT ButtonGroup_QMLTYPE_124::editModesObj
              SIGNALS OUT
                    signal: destroyed(QObject*)
                      --> QQuickColumnLayout::unnamed _q_resourceObjectDeleted(QObject*)
              SIGNALS IN
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
                      <-- RadioButton_QMLTYPE_126::unnamed <unknown>
            

            ... and my C++ connects successfully to the ButtonGroup's destroyed() signal. My QML also declares a Slider, and my C++ can connect to its valueChanged() signal without problem. But dumpObjectInfo() for the slider does not mention the valueChanged() signal - but a signal "baselineOffsetChanged(double) - now I'm really confused!

            OBJECT Slider_QMLTYPE_7::pingStepSliderObj
              SIGNALS OUT
                    signal: baselineOffsetChanged(double)
                      <functor or function pointer>
                      --> QQuickColumnLayout::unnamed invalidateSenderItem()
              SIGNALS IN
                      <-- Slider_QMLTYPE_7::pingStepSliderObj <unknown>
            
            B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #6

            @Tom-asso Oh, sorry my bad, I thought this function prints all signals, but actually it only prints the signals that already got connected.
            You can write code to print the signals though.
            Here we start i from meta->methodOffset() so it only prints signals defined in its class, if start from 0, then it will also print signals from superclasses.

            const QMetaObject* meta = obj->metaObject();
            for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) {
                QMetaMethod method = meta->method(i);
                if(method.methodType() == QMetaMethod::Signal) {
                    qDebug() << method.methodSignature();
                }
            }
            
            Tom assoT 1 Reply Last reply
            0
            • S Offline
              S Offline
              Sivan
              wrote on last edited by
              #7

              As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be

              QObject::connect((QObject *)obj, 
                             SIGNAL(clicked(QAbstractButton*)),
                             &mainWindow, SLOT(onButtonClicked()));
              
              Pl45m4P 1 Reply Last reply
              1
              • S Sivan

                As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be

                QObject::connect((QObject *)obj, 
                               SIGNAL(clicked(QAbstractButton*)),
                               &mainWindow, SLOT(onButtonClicked()));
                
                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #8

                @Sivan said in How to connect with ButtonGroup clicked(AbstractButton) signal?:

                As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be

                    QObject::connect((QObject *)obj, 
                          SIGNAL(clicked(QAbstractButton*)),
                          &mainWindow, SLOT(onButtonClicked()));
                

                What could have been avoided, when using the function pointer based connection style...

                Edit:
                @Sivan OP is using the QMLs AbstractButton, not QtWidgets QAbstractButton class


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                GrecKoG Tom assoT 2 Replies Last reply
                2
                • Pl45m4P Pl45m4

                  @Sivan said in How to connect with ButtonGroup clicked(AbstractButton) signal?:

                  As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be

                      QObject::connect((QObject *)obj, 
                            SIGNAL(clicked(QAbstractButton*)),
                            &mainWindow, SLOT(onButtonClicked()));
                  

                  What could have been avoided, when using the function pointer based connection style...

                  Edit:
                  @Sivan OP is using the QMLs AbstractButton, not QtWidgets QAbstractButton class

                  GrecKoG Offline
                  GrecKoG Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on last edited by
                  #9

                  A cleaner solution would be to expose some of your c++ objects to the QML and call one of its slots from QML when a button is clicked.

                  Don't connect to QML signals from c++.
                  https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
                  https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
                  https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
                  https://youtu.be/vzs5VPTf4QQ?t=23m20s

                  Tom assoT 1 Reply Last reply
                  2
                  • GrecKoG GrecKo

                    A cleaner solution would be to expose some of your c++ objects to the QML and call one of its slots from QML when a button is clicked.

                    Don't connect to QML signals from c++.
                    https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
                    https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
                    https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
                    https://youtu.be/vzs5VPTf4QQ?t=23m20s

                    Tom assoT Offline
                    Tom assoT Offline
                    Tom asso
                    wrote on last edited by
                    #10

                    @GrecKo - yes I'm convinced of that now too.

                    1 Reply Last reply
                    0
                    • B Bonnie

                      @Tom-asso Oh, sorry my bad, I thought this function prints all signals, but actually it only prints the signals that already got connected.
                      You can write code to print the signals though.
                      Here we start i from meta->methodOffset() so it only prints signals defined in its class, if start from 0, then it will also print signals from superclasses.

                      const QMetaObject* meta = obj->metaObject();
                      for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) {
                          QMetaMethod method = meta->method(i);
                          if(method.methodType() == QMetaMethod::Signal) {
                              qDebug() << method.methodSignature();
                          }
                      }
                      
                      Tom assoT Offline
                      Tom assoT Offline
                      Tom asso
                      wrote on last edited by
                      #11

                      @Bonnie - thanks that's very useful.

                      1 Reply Last reply
                      0
                      • Pl45m4P Pl45m4

                        @Sivan said in How to connect with ButtonGroup clicked(AbstractButton) signal?:

                        As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be

                            QObject::connect((QObject *)obj, 
                                  SIGNAL(clicked(QAbstractButton*)),
                                  &mainWindow, SLOT(onButtonClicked()));
                        

                        What could have been avoided, when using the function pointer based connection style...

                        Edit:
                        @Sivan OP is using the QMLs AbstractButton, not QtWidgets QAbstractButton class

                        Tom assoT Offline
                        Tom assoT Offline
                        Tom asso
                        wrote on last edited by
                        #12

                        @Pl45m4 - I didn't know about that function-pointer method; much better to catch errors at compile time!

                        1 Reply Last reply
                        0

                        • Login

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