<?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[setContextProperty not setting context properly]]></title><description><![CDATA[<p dir="auto">Okay, the way I understand it, I have to set the context properties before I load a qml file.  So I did this:</p>
<pre><code>int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    //QQuickView view(QUrl(QStringLiteral("qrc:/main.qml")));
    //QQuickItem *item = view.rootObject();

    Controller control; // qobject based

    QQmlApplicationEngine engine;

    QQmlContext *context = new QQmlContext(engine.rootContext());
    context-&gt;setContextProperty("controller", &amp;control);

    //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    //engine.load(QUrl());
    //if (engine.rootObjects().isEmpty())
    //    return -1;
    QQmlComponent component(&amp;engine);
    component.loadUrl(QUrl(QStringLiteral("qrc:/main.qml")));

    QQuickItem *tobj;

    if (component.isReady()){
        QObject *myObject = component.create();
        tobj = qobject_cast&lt;QQuickItem*&gt;(myObject);
    }else
        qWarning() &lt;&lt; component.errorString();

    //QQuickItem *item = engine.rootObjects();
    for(auto item: engine.rootObjects())
    {
        QObject::connect(item, SIGNAL(operateSig()),
                             &amp;control, SIGNAL(operate()));
        break;
    }

    return app.exec();
}
</code></pre>
<p dir="auto">QML file:</p>
<pre><code>import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 1.4

/*
  This program is for testing out OPC UA interfaces.
  */

Window {
    id: mainWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("OPC UA Playground")

    signal operateSig()

    Connections {
        target: controller
        onStatus: {
            console.log(message);
        }
    }

    Button {
        id: operateButton
        text: "Operate"
        onClicked: {
            mainWindow.operateSig();
        }
    }

}
</code></pre>
<p dir="auto">However, I get these errors:</p>
<pre><code>qrc:/main.qml:18:5: QML Connections: Cannot assign to non-existent property "onStatus"
qrc:/main.qml:19: ReferenceError: controller is not defined
</code></pre>
<p dir="auto">I don't understand why it cannot see the "controller" object.  I have set contexts before in other apps and it has worked.  I have to be missing something really  really simple.</p>
]]></description><link>https://forum.qt.io/topic/101344/setcontextproperty-not-setting-context-properly</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 00:17:01 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/101344.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Apr 2019 16:24:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to setContextProperty not setting context properly on Tue, 02 Apr 2019 17:27:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> said in <a href="/post/520844">setContextProperty not setting context properly</a>:</p>
<blockquote>
<p dir="auto">You are constructing a new context, which is not the same as the one your engine is running on.</p>
</blockquote>
<p dir="auto">Thank you! I got that from too much copy pasta trying to figure out how to do this.  All my errors are gone now.</p>
]]></description><link>https://forum.qt.io/post/520849</link><guid isPermaLink="true">https://forum.qt.io/post/520849</guid><dc:creator><![CDATA[fcarney]]></dc:creator><pubDate>Tue, 02 Apr 2019 17:27:18 GMT</pubDate></item><item><title><![CDATA[Reply to setContextProperty not setting context properly on Tue, 02 Apr 2019 16:56:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/fcarney">@<bdi>fcarney</bdi></a> said in <a href="/post/520841">setContextProperty not setting context properly</a>:</p>
<blockquote>
<p dir="auto">QQmlContext *context = new QQmlContext(engine.rootContext());</p>
</blockquote>
<p dir="auto">You are constructing a new context, which is not the same as the one your engine is running on.</p>
<p dir="auto">Do this instead:</p>
<pre><code>QQmlContext *context = engine.rootContext();
context-&gt;setContextProperty("controller", &amp;control);
</code></pre>
<p dir="auto">Or simpler:</p>
<pre><code>engine.rootContext()-&gt;setContextProperty("controller", &amp;control);
</code></pre>
<p dir="auto">And then <code>engine.load("qrc:/main.qml");</code> of course.</p>
]]></description><link>https://forum.qt.io/post/520844</link><guid isPermaLink="true">https://forum.qt.io/post/520844</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Tue, 02 Apr 2019 16:56:21 GMT</pubDate></item></channel></rss>