<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Access rootObject from c++ file other than main.cpp]]></title><description><![CDATA[<p dir="auto">Hi</p>
<p dir="auto">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.</p>
<p dir="auto">this is the code for it</p>
<pre><code>#include &lt;QGuiApplication&gt;
#include &lt;QQmlApplicationEngine&gt;
#include &lt;qqmlengine.h&gt;
#include &lt;qqmlcontext.h&gt;
#include &lt;qqml.h&gt;
#include &lt;QQmlComponent&gt;
#include &lt;QStringList&gt;
#include "connect_serial_port.h"
#include "tcpip_client.h"
#include "fileio.h"
#include "cmd_do.h"
#include &lt;QDebug&gt;

//#include "ui_settings.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    qmlRegisterType&lt;FileIO, 1&gt;("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-&gt;setContextProperty("connect_serial_port", &amp;connect_serial_port);
    classContext-&gt;setContextProperty("tcpip_client", &amp;tcpip_client);
    classContext-&gt;setContextProperty("cmd_do", &amp;cmd_do);
    //classContext-&gt;setContextProperty("Ui_Settings",&amp;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-&gt;findChild&lt;QObject*&gt;("serial_radio");
        QObject *qmlObject_tcpip_radio = rootObject-&gt;findChild&lt;QObject*&gt;("tcpip_radio");
    // Step 2a: set or get the desired property value for the root object
        rootObject-&gt;setProperty("visible", true);
        rootObject-&gt;setProperty("width", "1200");
        qDebug() &lt;&lt; rootObject-&gt;property("visible");
        qDebug() &lt;&lt; qmlObject_serial_radio-&gt;property("checked");
         qDebug() &lt;&lt; qmlObject_tcpip_radio-&gt;property("checked");








    connect_serial_port.scan_for_ports();

    return app.exec();
}
</code></pre>
<p dir="auto">but how do I do the same in a different c++ file other than main.cpp.<br />
how t o access rootObject and all of its child?</p>
]]></description><link>https://forum.qt.io/topic/93178/access-rootobject-from-c-file-other-than-main-cpp</link><generator>RSS for Node</generator><lastBuildDate>Mon, 11 May 2026 06:17:33 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/93178.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 Jul 2018 13:38:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Access rootObject from c++ file other than main.cpp on Mon, 30 Jul 2018 14:51:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/raven-worx">@<bdi>raven-worx</bdi></a></p>
<p dir="auto">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.<br />
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.</p>
<p dir="auto">but in the above case I am a bit confused how to do it.</p>
<p dir="auto">and One more question when I toggle the radio buttons how do I know it is updated inthe QObject tree ?<br />
in matlab i used to do something like this guidata(fig_hdl, handles);</p>
]]></description><link>https://forum.qt.io/post/472804</link><guid isPermaLink="true">https://forum.qt.io/post/472804</guid><dc:creator><![CDATA[Prasad_Socionext]]></dc:creator><pubDate>Mon, 30 Jul 2018 14:51:58 GMT</pubDate></item><item><title><![CDATA[Reply to Access rootObject from c++ file other than main.cpp on Mon, 30 Jul 2018 13:58:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/prasad_socionext">@<bdi>Prasad_Socionext</bdi></a><br />
either you pass a pointer to the engine across your cpp files.</p>
<p dir="auto">Or if you have a pointer to an QML item you can also do the following:</p>
<pre><code>QQmlEngine* engine = qmlEngine(itemInstancePtr);
</code></pre>
]]></description><link>https://forum.qt.io/post/472797</link><guid isPermaLink="true">https://forum.qt.io/post/472797</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Mon, 30 Jul 2018 13:58:55 GMT</pubDate></item></channel></rss>