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. C++ can't find ButtonGroup?

C++ can't find ButtonGroup?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 399 Views
  • 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 Offline
    Tom assoT Offline
    Tom asso
    wrote on last edited by Tom asso
    #1

    I'm running Qt 5.15.
    My C++ app cannot find a ButtonGroup declared with an objectName in QML. The radio buttons in the group appear to function properly and the C++ app is able to find all other objectNames in QML, but not the one for ButtonGroup.

    main.qml:

    ButtonGroup {
          id: editModes
          objectName: "editModesObj"
        }
    
        Row {
           id: buttonRow
           
            RadioButton {
              checked: true
              text: qsTr("Male")
              ButtonGroup.group: editModes
             }
    
            RadioButton {
              text: qsTr("Female")
              ButtonGroup.group: editModes    
            }
    

    main.cpp:

    #define EDIT_MODES_NAME “editModesObj”
    
    QButtonGroup *obj =
          rootObject->findChild<QButtonGroup*>(EDIT_MODES_NAME);
    
    if (!obj) {
          qWarning() << "Couldn't find QML object named " << EDIT_MODES_NAME;
    }
    

    The result of the above call to findChild() is null for "editModesObj". What am I doing wrong? Should I expect a non-visual object like ButtonGroup to be find-able by findChild()? If not, how can my C++ connect the ButtonGroup signal to a slot?
    Thanks!

    C 1 Reply Last reply
    0
    • Tom assoT Tom asso

      I'm running Qt 5.15.
      My C++ app cannot find a ButtonGroup declared with an objectName in QML. The radio buttons in the group appear to function properly and the C++ app is able to find all other objectNames in QML, but not the one for ButtonGroup.

      main.qml:

      ButtonGroup {
            id: editModes
            objectName: "editModesObj"
          }
      
          Row {
             id: buttonRow
             
              RadioButton {
                checked: true
                text: qsTr("Male")
                ButtonGroup.group: editModes
               }
      
              RadioButton {
                text: qsTr("Female")
                ButtonGroup.group: editModes    
              }
      

      main.cpp:

      #define EDIT_MODES_NAME “editModesObj”
      
      QButtonGroup *obj =
            rootObject->findChild<QButtonGroup*>(EDIT_MODES_NAME);
      
      if (!obj) {
            qWarning() << "Couldn't find QML object named " << EDIT_MODES_NAME;
      }
      

      The result of the above call to findChild() is null for "editModesObj". What am I doing wrong? Should I expect a non-visual object like ButtonGroup to be find-able by findChild()? If not, how can my C++ connect the ButtonGroup signal to a slot?
      Thanks!

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

      I suspect that the ButtonGroup QML object expose a QObject interface, not a QButtonGroup

      QObject *obj =
            rootObject->findChild<QObject*>(EDIT_MODES_NAME);
      

      Read the warning here though.

      Read here to understand how QML signals are automatically available in C++.

      Tom assoT 1 Reply Last reply
      1
      • C ChrisW67

        I suspect that the ButtonGroup QML object expose a QObject interface, not a QButtonGroup

        QObject *obj =
              rootObject->findChild<QObject*>(EDIT_MODES_NAME);
        

        Read the warning here though.

        Read here to understand how QML signals are automatically available in C++.

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

        @ChrisW67

        | I suspect that the ButtonGroup QML object expose a QObject | |
        | interface, not a QButtonGroup

        Yes, findChild<QObject*> finds the ButtonGroup!
        Brilliant! How in the world did you know that?
        Regarding the Warning about 'manipulating' QML from C++, my C++ code is only establishing a slot for the ButtonGroup signal, which seems a pretty standard way to do things in the Qt world.

        C 1 Reply Last reply
        0
        • Tom assoT Tom asso

          @ChrisW67

          | I suspect that the ButtonGroup QML object expose a QObject | |
          | interface, not a QButtonGroup

          Yes, findChild<QObject*> finds the ButtonGroup!
          Brilliant! How in the world did you know that?
          Regarding the Warning about 'manipulating' QML from C++, my C++ code is only establishing a slot for the ButtonGroup signal, which seems a pretty standard way to do things in the Qt world.

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

          @Tom-asso said in C++ can't find ButtonGroup?:

          Brilliant! How in the world did you know that?

          The example immediately above the warning.

          Regarding the Warning about 'manipulating' QML from C++, my C++ code is only establishing a slot for the ButtonGroup signal, which seems a pretty standard way to do things in the Qt world.

          Yes. Using the standard QObject mechanisms is OK. Doing something like this (from a little further down the same page) is not:

          //bad code
          QQmlComponent component(engine, "MyButton.qml");
          PushButton *button = qobject_cast<PushButton*>(component.create());
          button->m_buttonText = "Click me";
          
          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