- I have two radio buttons in one panel defined in one .qml file.
- I need to access the property whether it is checked or not in another QML file or in .cpp file of some c++ class.
- I am able to do it in main.cpp
using these lines below
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if(engine.rootObjects().isEmpty())
return -1;
// Step 1: get access to the root object
QObject *rootObject = engine.rootObjects().first();
QObject *qmlObject_serial_radio = rootObject->findChild<QObject*>("serial_radio");
QObject *qmlObject_tcpip_radio = rootObject->findChild<QObject*>("tcpip_radio");
// Step 2a: set or get the desired property value for the root object
qDebug() << qmlObject_serial_radio->property("checked");
qDebug() << qmlObject_tcpip_radio->property("checked");
But I want to do the same in some other .cpp file.
how to do it?
Instancing QML objects in C++ can be dangerous since the life cycle of the items is not handled directly in C++ so QML could eliminate them without notifying it as for example the StackView where Pages are created and deleted.
A better approach is to export the C ++ object to the QML:
main.cpp
main.qml