C++ can't find ButtonGroup?
-
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! -
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! -
| I suspect that the ButtonGroup QML object expose a QObject | |
| interface, not a QButtonGroupYes, 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. -
| I suspect that the ButtonGroup QML object expose a QObject | |
| interface, not a QButtonGroupYes, 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.@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";