<?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[Signal not emitted from C++ to QML]]></title><description><![CDATA[<p dir="auto">Hi all</p>
<p dir="auto">I have created a qml project using a C++ plugin (Torch). A simplified version of the code is pasted below.</p>
<p dir="auto">Torch offers a Q_PROPERTY flashTime.</p>
<p dir="auto">If I set this property from QML (e.g. via the button on MainPage), then I receive the onFlashTimeChanged signal in QML. (OK)</p>
<p dir="auto">However if I change this property from within C++ by the setFlashTime function (e.g. called from the class constructor), then in QML I do not receive the onFlashTimeChanged signal. (NOT OK).</p>
<p dir="auto">I have been running this on Harmattan on a Nokia N9.</p>
<p dir="auto">Any ideas? I am sure I have missed something small ...</p>
<p dir="auto">Chris</p>
<p dir="auto">@<a href="//Torch.h" target="_blank" rel="noopener noreferrer nofollow ugc">//Torch.h</a><br />
#ifndef TORCH_H<br />
#define TORCH_H</p>
<p dir="auto">#include &lt;QDeclarativeItem&gt;<br />
#include &lt;QDebug&gt;</p>
<p dir="auto">class Torch : public QDeclarativeItem<br />
{<br />
Q_OBJECT<br />
Q_PROPERTY(int flashTime READ flashTime WRITE setFlashTime NOTIFY flashTimeChanged)<br />
public:<br />
explicit Torch(QDeclarativeItem *parent = 0);<br />
~Torch();<br />
int flashTime() const;<br />
void setFlashTime(int flashTime);</p>
<p dir="auto">signals:<br />
void flashTimeChanged(const int &amp;flashTime);</p>
<p dir="auto">public slots:</p>
<p dir="auto">protected slots:</p>
<p dir="auto">private:<br />
int _flashTime;<br />
}<br />
#endif // TORCH_H;@</p>
<p dir="auto">@<a href="//Torch.cpp" target="_blank" rel="noopener noreferrer nofollow ugc">//Torch.cpp</a><br />
#include "Torch.h"<br />
#include &lt;QDebug&gt;</p>
<p dir="auto">Torch::Torch(QDeclarativeItem <em>parent) :<br />
QDeclarativeItem(parent)<br />
,_flashTime(0.75</em>1000)<br />
{<br />
setFlashTime(760);<br />
}</p>
<p dir="auto">Torch::~Torch()<br />
{<br />
}</p>
<p dir="auto">int Torch::flashTime() const<br />
{<br />
return _flashTime;<br />
}</p>
<p dir="auto">void Torch::setFlashTime(int flashTime)<br />
{<br />
qDebug() &lt;&lt; "c++ setFlashTime called";<br />
if (flashTime == _flashTime)<br />
{<br />
qDebug() &lt;&lt; "value is same ...";<br />
return;<br />
}<br />
qDebug() &lt;&lt; "about to emit flashTimeChanged";<br />
_flashTime = flashTime;<br />
emit flashTimeChanged(flashTime);<br />
};@</p>
<p dir="auto">@<a href="//main.cpp" target="_blank" rel="noopener noreferrer nofollow ugc">//main.cpp</a><br />
#include &lt;QApplication&gt;<br />
#include "qmlapplicationviewer.h"<br />
#include "Torch.h"</p>
<p dir="auto">Q_DECL_EXPORT int main(int argc, char *argv[])<br />
{<br />
QScopedPointer&lt;QApplication&gt; app(createApplication(argc, argv));<br />
qmlRegisterType&lt;Torch&gt;("Torch",1,0,"Torch");<br />
QmlApplicationViewer viewer;<br />
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);<br />
viewer.setMainQmlFile(QLatin1String("qml/flashTime/main.qml"));<br />
viewer.showExpanded();</p>
<pre><code>return app-&gt;exec&amp;#40;&amp;#41;;
</code></pre>
<p dir="auto">};@</p>
<p dir="auto">@<a href="//main.qml" target="_blank" rel="noopener noreferrer nofollow ugc">//main.qml</a><br />
import QtQuick 1.1<br />
import com.nokia.meego 1.0</p>
<p dir="auto">PageStackWindow {<br />
id: appWindow<br />
initialPage: mainPage<br />
MainPage {<br />
id: mainPage<br />
}<br />
};@</p>
<p dir="auto">@<a href="//MainPage.qml" target="_blank" rel="noopener noreferrer nofollow ugc">//MainPage.qml</a><br />
import QtQuick 1.1<br />
import com.nokia.meego 1.0<br />
import Torch 1.0</p>
<p dir="auto">Page {<br />
id: page</p>
<pre><code>Torch {
    id: torch
    onFlashTimeChanged: console.log("FlashTimeChanged: " + flashTime)
}

Button {
    y: 100
    text: "double flashTime"
    onClicked: torch.flashTime = torch.flashTime * 2;
}
</code></pre>
<p dir="auto">};@</p>
]]></description><link>https://forum.qt.io/topic/34342/signal-not-emitted-from-c-to-qml</link><generator>RSS for Node</generator><lastBuildDate>Sat, 14 Nov 2026 23:56:38 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/34342.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 15 Nov 2013 14:41:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Signal not emitted from C++ to QML on Mon, 18 Nov 2013 13:13:10 GMT]]></title><description><![CDATA[<p dir="auto">Have you also tried calling the method outside from the constructor? It may be the case that the bindings are not already up and working at this place. As an alternative you can try to call <em>setFlashTime</em> from <em>componentComplete</em> (a virtual method you can override in your <em>Torch</em> class).</p>
<p dir="auto">Alex</p>
]]></description><link>https://forum.qt.io/post/204422</link><guid isPermaLink="true">https://forum.qt.io/post/204422</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 18 Nov 2013 13:13:10 GMT</pubDate></item></channel></rss>