<?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[Setting up a slot for QmlApplicationViewer]]></title><description><![CDATA[<p dir="auto">Hi there,</p>
<p dir="auto">I've been trying to find a solution for this the past few hours and I'm kind of stuck here. I have a main.cpp file which loads my qml file like this:</p>
<p dir="auto">@QmlApplicationViewer viewer;<br />
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);<br />
viewer.setMainQmlFile(QLatin1String("qml/test/main.qml"));<br />
viewer.showExpanded();@</p>
<p dir="auto">So nothing special. Also, I have a QThread that's running and which is emitting signals. Now I try to set up a slot (or something else) that calls a function in my qml file. I tried to add a slot into qmlapplicationviewer.h and implemented it in qmlapplicationviewer.cpp like that:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/void">@<bdi>void</bdi></a> QmlApplicationViewer::zoom(int scale){<br />
QVariant sc = scale;<br />
QObject* mainItem = this-&gt;findChild&lt;QObject *&gt;("mainItem");<br />
QMetaObject::invokeMethod(mainItem, "zoomIn", Q_ARG(QVariant, sc));<br />
}@</p>
<p dir="auto">In main.cpp I connect the signal to the slot:</p>
<p dir="auto">@QObject::connect( canThread, SIGNAL( zoom(int) ), &amp;viewer, SLOT( zoom(int) ));@</p>
<p dir="auto">And this is my main.qml:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> QtQuick 1.1</p>
<p dir="auto">Rectangle{<br />
id: mainItem<br />
objectName: "mainItem"<br />
...</p>
<pre><code>function zoomIn(steps) {
    bg.scale *= 2;
}
</code></pre>
<p dir="auto">}@</p>
<p dir="auto">The signal is emitted correctly by my thread and also the slot gets called. Nevertheless, the function in qml is never executed</p>
<p dir="auto">I'm no real c++ crack so maybe someone can figure out where I run into problems.</p>
<p dir="auto">Thanks a lot!</p>
]]></description><link>https://forum.qt.io/topic/18536/setting-up-a-slot-for-qmlapplicationviewer</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Jul 2026 08:51:18 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/18536.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 24 Jul 2012 09:34:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Setting up a slot for QmlApplicationViewer on Thu, 06 Sep 2012 08:11:12 GMT]]></title><description><![CDATA[<p dir="auto">OK, the reason for the problem is, that findChild() does not check "id", it looks for "objectName" property. Thanks to <a href="http://www.lothlorien.com/kf6gpe/?p=309" target="_blank" rel="noopener noreferrer nofollow ugc">http://www.lothlorien.com/kf6gpe/?p=309</a> . Moreover, invokeMethod() should be called after setMainQmlFile() and must not be trigered from QML (i.e. qml calls C++ function which calls javascript function) before it finishes the initialization (not even from Component.onCompleted)</p>
<p dir="auto">@<br />
QMetaObject::invokeMethod(<br />
viewer-&gt;rootObject()-&gt;findChild&lt;QObject *&gt;("myModel"), // QML item<br />
"test"                                                 // method<br />
// parameters<br />
);<br />
@<br />
@<br />
PageStackWindow {<br />
id: mainApp<br />
...</p>
<pre><code>ListModel {
    id: myModel
    objectName: myModel
    ...
    function test() {
        console.log("TEST OK");
    }
}
 
Page {
    ...
}
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/post/149700</link><guid isPermaLink="true">https://forum.qt.io/post/149700</guid><dc:creator><![CDATA[meolic]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:11:12 GMT</pubDate></item><item><title><![CDATA[Reply to Setting up a slot for QmlApplicationViewer on Wed, 05 Sep 2012 08:37:31 GMT]]></title><description><![CDATA[<p dir="auto">It seems to me that your solution is<br />
@<br />
QMetaObject::invokeMethod(<br />
(QObject *)viewer-&gt;rootObject(), // for this object we will call method<br />
"test"                           // name of the method to call<br />
// without parameters<br />
);<br />
@<br />
@<br />
PageStackWindow {<br />
id: mainApp<br />
...<br />
function test() {<br />
myModel.test()<br />
}</p>
<pre><code>ListModel {
    id: myModel
    ...
    function test() {
        console.log("TEST OK");
    }
}

Page {
    ...
}
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">However, I am interested in more elegant solution.</p>
]]></description><link>https://forum.qt.io/post/149570</link><guid isPermaLink="true">https://forum.qt.io/post/149570</guid><dc:creator><![CDATA[meolic]]></dc:creator><pubDate>Wed, 05 Sep 2012 08:37:31 GMT</pubDate></item><item><title><![CDATA[Reply to Setting up a slot for QmlApplicationViewer on Wed, 25 Jul 2012 16:19:42 GMT]]></title><description><![CDATA[<p dir="auto">I couldn't figure out how to solve this, so I made a little wordaround.</p>
<p dir="auto">I coded a parser class which takes the pointer from:</p>
<p dir="auto">@(QObject *)viewer.rootObject();@</p>
<p dir="auto">This works when putting all functions to your root object...</p>
]]></description><link>https://forum.qt.io/post/145581</link><guid isPermaLink="true">https://forum.qt.io/post/145581</guid><dc:creator><![CDATA[TilmanK]]></dc:creator><pubDate>Wed, 25 Jul 2012 16:19:42 GMT</pubDate></item><item><title><![CDATA[Reply to Setting up a slot for QmlApplicationViewer on Wed, 25 Jul 2012 00:52:18 GMT]]></title><description><![CDATA[<p dir="auto">Try naming them differently, it will help the debugging process so you can examine them individually and have better clarity as to what you are looking at and what is causing the problem</p>
]]></description><link>https://forum.qt.io/post/145470</link><guid isPermaLink="true">https://forum.qt.io/post/145470</guid><dc:creator><![CDATA[bluestreak]]></dc:creator><pubDate>Wed, 25 Jul 2012 00:52:18 GMT</pubDate></item><item><title><![CDATA[Reply to Setting up a slot for QmlApplicationViewer on Tue, 24 Jul 2012 11:14:24 GMT]]></title><description><![CDATA[<p dir="auto">There is only one "mainItem", yes. But, the pointer "mainItem" in my slot is indeed 0. But why?</p>
]]></description><link>https://forum.qt.io/post/145402</link><guid isPermaLink="true">https://forum.qt.io/post/145402</guid><dc:creator><![CDATA[TilmanK]]></dc:creator><pubDate>Tue, 24 Jul 2012 11:14:24 GMT</pubDate></item><item><title><![CDATA[Reply to Setting up a slot for QmlApplicationViewer on Tue, 24 Jul 2012 09:53:33 GMT]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">May i ask you if :<br />
Did you have only one object of objectName: "mainItem" ?<br />
Did you check if mainItem is not null?</p>
]]></description><link>https://forum.qt.io/post/145398</link><guid isPermaLink="true">https://forum.qt.io/post/145398</guid><dc:creator><![CDATA[dmcr]]></dc:creator><pubDate>Tue, 24 Jul 2012 09:53:33 GMT</pubDate></item></channel></rss>