<?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[Doubt with the Qt5 documentation related to Controller class and QThread]]></title><description><![CDATA[<p dir="auto">Dear all,</p>
<p dir="auto">I'm trying to understand the code below (extracted from <a href="http://qt-project.org/doc/qt-5/qthread.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/qthread.html</a>).</p>
<p dir="auto">@<br />
class Worker : public QObject<br />
{<br />
Q_OBJECT</p>
<p dir="auto">public slots:<br />
void doWork(const QString &amp;parameter) {<br />
QString result;<br />
/* ... here is the expensive or blocking operation ... */<br />
emit resultReady(result);<br />
}</p>
<p dir="auto">signals:<br />
void resultReady(const QString &amp;result);<br />
};</p>
<p dir="auto">class Controller : public QObject<br />
{<br />
Q_OBJECT<br />
QThread workerThread;<br />
public:<br />
Controller() {<br />
Worker *worker = new Worker;<br />
worker-&gt;moveToThread(&amp;workerThread);<br />
connect(&amp;workerThread, &amp;QThread::finished, worker, &amp;QObject::deleteLater);<br />
connect(this, &amp;Controller::operate, worker, &amp;Worker::doWork);<br />
connect(worker, &amp;Worker::resultReady, this, &amp;Controller::handleResults);<br />
workerThread.start();<br />
}<br />
~Controller() {<br />
workerThread.quit();<br />
workerThread.wait();<br />
}<br />
public slots:<br />
void handleResults(const QString &amp;);<br />
signals:<br />
void operate(const QString &amp;);<br />
};<br />
@</p>
<p dir="auto">Can someone explain me who/where the operate signal is emitted? Shall I emit operate() signal to launch the thread? If yes, why do I get the following error: Cannot send events to objects owned by a different thread.... ?</p>
<p dir="auto">Many thanks</p>
<p dir="auto"><em>[edit: Added missing coding tags @ SGaist]</em></p>
]]></description><link>https://forum.qt.io/topic/46390/doubt-with-the-qt5-documentation-related-to-controller-class-and-qthread</link><generator>RSS for Node</generator><lastBuildDate>Sun, 14 Jun 2026 15:51:04 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/46390.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 26 Sep 2014 14:28:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Doubt with the Qt5 documentation related to Controller class and QThread on Mon, 29 Sep 2014 12:27:08 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">How did you emit the <em>Controller::operate()</em> signal?</p>
<p dir="auto">In that example, when <em>Controller::operate()</em> is emitted, the <em>Worker::doWork()</em> slot will run in your secondary thread.</p>
<p dir="auto">Note that you don't need to use that Controller object. The documentation also says, <em>"You are free to connect the Worker's slots to any signal, from any object, in any thread"</em>. All you need to do is connect <em>any</em> signal to <em>Worker::doWork()</em>, and then emit that signal.</p>
]]></description><link>https://forum.qt.io/post/245659</link><guid isPermaLink="true">https://forum.qt.io/post/245659</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Mon, 29 Sep 2014 12:27:08 GMT</pubDate></item></channel></rss>