Unsolved Access rootObject from c++ file other than main.cpp
-
Hi
I need to get access for some QML object property for example two radio button from c++ file , I know how to do it in main.cpp.
this is the code for it
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <qqmlengine.h> #include <qqmlcontext.h> #include <qqml.h> #include <QQmlComponent> #include <QStringList> #include "connect_serial_port.h" #include "tcpip_client.h" #include "fileio.h" #include "cmd_do.h" #include <QDebug> //#include "ui_settings.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); qmlRegisterType<FileIO, 1>("FileIO", 1, 0, "FileIO"); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; Connect_Serial_Port connect_serial_port; Tcpip_Client tcpip_client; Cmd_Do cmd_do; //Ui_Settings ui_settings; QQmlContext *classContext = engine.rootContext(); classContext->setContextProperty("connect_serial_port", &connect_serial_port); classContext->setContextProperty("tcpip_client", &tcpip_client); classContext->setContextProperty("cmd_do", &cmd_do); //classContext->setContextProperty("Ui_Settings",&ui_settings); 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 rootObject->setProperty("visible", true); rootObject->setProperty("width", "1200"); qDebug() << rootObject->property("visible"); qDebug() << qmlObject_serial_radio->property("checked"); qDebug() << qmlObject_tcpip_radio->property("checked"); connect_serial_port.scan_for_ports(); return app.exec(); }
but how do I do the same in a different c++ file other than main.cpp.
how t o access rootObject and all of its child? -
@Prasad_Socionext
either you pass a pointer to the engine across your cpp files.Or if you have a pointer to an QML item you can also do the following:
QQmlEngine* engine = qmlEngine(itemInstancePtr);
-
Hi @raven-worx
Thanks for your reply. I am clicking a button in another panel. which should do something based on the radio button chosen in another panel.
if i am invoking a method which accesses the property of the items in the same panel , i guess i can send the object pointer as parameters to cpp.but in the above case I am a bit confused how to do it.
and One more question when I toggle the radio buttons how do I know it is updated inthe QObject tree ?
in matlab i used to do something like this guidata(fig_hdl, handles);