<?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[Invoking a slot from a non-Qt thread]]></title><description><![CDATA[<p dir="auto">I correct to think that there's only two ways to invoke a slot from a non-QObject based thread?</p>
<ul>
<li>
<p dir="auto">Use QMetaObject::invokeMethod(), or</p>
</li>
<li>
<p dir="auto">emit a signal (can I do that on a non Qt thread?)</p>
</li>
</ul>
<p dir="auto">Thanks<br />
David</p>
]]></description><link>https://forum.qt.io/topic/162011/invoking-a-slot-from-a-non-qt-thread</link><generator>RSS for Node</generator><lastBuildDate>Mon, 06 Apr 2026 22:54:37 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/162011.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 03 May 2025 10:11:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Mon, 05 May 2025 07:12:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/axel-spoerl">@<bdi>Axel-Spoerl</bdi></a> said in <a href="/post/825675">Invoking a slot from a non-Qt thread</a>:</p>
<blockquote>
<p dir="auto">I'd probably go for an interface class, living inside the std::thread, inheriting from QObject and running its own QEventLoop.</p>
</blockquote>
<p dir="auto">From what I understand only the calling thread does not have anything Qt-based. In order to emit a signals there is no need to start an event loop inside that non-Qt thread. Only a receiving slot would require an event loop. Or am I wrong about this?</p>
<p dir="auto">The quickest way would certainly be using invokeMethod. However, if you can (and are willing to) using signals is always better. If you don't want to introduce any Qt into that thread (yet) you can have a regular C++ function inside some existing Qt code and call that. This function can either use invokeMethod or even emit a signal for your.</p>
<p dir="auto">If it is inevitable to keep Qt out of your thread (in the long run) it makes sense to create something derived from QObject to be used inside your non-Qt thread. Then you can emit a signal using this object. I don't think you need anything special in the calling thread. <code>emit</code> is just a Qt keyword that gets removed by the preprocessor. From the compiler's point of view emitting a signal is just calling a regular member function. (The member function for the signal is generated by <code>moc</code>.) You need to be careful about object lifetimes. The calling object needs to live long enough for the slot to be executed. If the calling or receiving object in a <code>connect</code> statement is deleted, all queued slot invocations are also removed. Also, as your non-Qt thread does not have an event loop you cannot use <code>deleteLater()</code> to delete the object. If you cannot guarantee that your non-Qt thread runs long enough for the slot to be executed (or rather at least started) inside the Qt thread (and thus cannot guarantee a long lifetime for the QObject-derived object) using <code>invokeMethod</code> is the safest way. Maybe it even makes the most sense to use <code>invokeMethod</code> if you don't rely on member variables of the calling object, but instead hand over all data necessary for the slot as function arguments. It certainly avoid thinking about object lifetimes (and thread lifetimes).</p>
]]></description><link>https://forum.qt.io/post/825731</link><guid isPermaLink="true">https://forum.qt.io/post/825731</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 05 May 2025 07:12:07 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Mon, 05 May 2025 06:21:45 GMT]]></title><description><![CDATA[<p dir="auto">I haven't tried anything, but can't you simply pass a qt signal als callback function? Any connects won't auto default to QtQueuedConnection as there's no thread info due to no QThread but you can manage that manually</p>
]]></description><link>https://forum.qt.io/post/825726</link><guid isPermaLink="true">https://forum.qt.io/post/825726</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Mon, 05 May 2025 06:21:45 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 13:11:09 GMT]]></title><description><![CDATA[<p dir="auto">Sounds like squeezing toothpaste back into the tube.<br />
I'd probably go for an interface class, living inside the std::thread, inheriting from <code>QObject</code> and running its own <code>QEventLoop</code>.<br />
That class can provide wrappers to either invoke methods in other threads, or emit signals.<br />
In the moment you could replace the whatever thread with a <code>QThread</code>, you can untangle the interface class and do the calls directly.</p>
]]></description><link>https://forum.qt.io/post/825675</link><guid isPermaLink="true">https://forum.qt.io/post/825675</guid><dc:creator><![CDATA[Axel Spoerl]]></dc:creator><pubDate>Sat, 03 May 2025 13:11:09 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 12:55:44 GMT]]></title><description><![CDATA[<p dir="auto">The application was not developed using Qt it was developed using MFC.   We have converted it to Qt over the last two to three years (it was a pretty large job).   Where it was helpful and expedient we did use QThreads, but there are other bits of threading that we've not (yet) got round to...</p>
]]></description><link>https://forum.qt.io/post/825673</link><guid isPermaLink="true">https://forum.qt.io/post/825673</guid><dc:creator><![CDATA[Perdrix]]></dc:creator><pubDate>Sat, 03 May 2025 12:55:44 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 12:49:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/axel-spoerl">@<bdi>Axel-Spoerl</bdi></a> said in <a href="/post/825671">Invoking a slot from a non-Qt thread</a>:</p>
<blockquote>
<p dir="auto">But if you develop an application with Qt, why not use QThread anyway?</p>
</blockquote>
<p dir="auto">I agree.  But don't you think Mr <a class="plugin-mentions-user plugin-mentions-a" href="/user/perdrix">@<bdi>Perdrix</bdi></a> is going to say "because it's already in someone else's code which I don't want to change but instead work with"? :)</p>
]]></description><link>https://forum.qt.io/post/825672</link><guid isPermaLink="true">https://forum.qt.io/post/825672</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 03 May 2025 12:49:18 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 12:13:24 GMT]]></title><description><![CDATA[<p dir="auto">In order to emit a signal or use <code>QMetaObject::invokeMethod()</code> from another thread, it doesn't technically have to be a <code>QThread</code>. What's required is an event loop and the emitter / invoker to inherit from <code>QObject</code>. But if you develop an application with Qt, why not use <code>QThread</code> anyway?</p>
<p dir="auto">We've seen an implementation in <a href="https://forum.qt.io/topic/161924/focus-stealing-progress-dialog">this topic</a>, that circumvents missing <code>QObject</code> inheritance / event loop. It implements methods in a <code>QObject</code> wrapping a <code>QMetaObject::invokeMethod()</code> call on itself. The caller just calls the wrapper from another thread. While it might technically work, I would clearly not recommend it. Instead, make the caller inherit from <code>QObject</code> and let it live in another <code>QThread</code>. All problems solved cleanly.</p>
]]></description><link>https://forum.qt.io/post/825671</link><guid isPermaLink="true">https://forum.qt.io/post/825671</guid><dc:creator><![CDATA[Axel Spoerl]]></dc:creator><pubDate>Sat, 03 May 2025 12:13:24 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 12:13:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/825669">Invoking a slot from a non-Qt thread</a>:</p>
<blockquote>
<p dir="auto">a non-Qt thread can use QMetaObject::invokeMethod() to call a slot if it has access to a receiving object but not a sending one to do a connect(). Right?</p>
</blockquote>
<p dir="auto">Yes. But imo it's better to define a signal in the receiver which then does the thread context switch.</p>
]]></description><link>https://forum.qt.io/post/825670</link><guid isPermaLink="true">https://forum.qt.io/post/825670</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 03 May 2025 12:13:23 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 12:04:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said in <a href="/post/825668">Invoking a slot from a non-Qt thread</a>:</p>
<blockquote>
<p dir="auto">Yes, but the question was if it can be invoked from a std::thread (or other non QThread based one). Nothing about what object it is 🙂</p>
</blockquote>
<p dir="auto">Oh indeed!  And the reference links are about that, e.g. the second one is about</p>
<blockquote>
<p dir="auto">I want to emit a signal from a C++ thread (std::thread) in Qt.</p>
</blockquote>
<p dir="auto">I just meant that OP is going to need to Qt objects around from somewhere in the thread.  I agree with you that <code>emit</code> signal is the obvious route.  But, just for the record, a non-Qt thread can use <code>QMetaObject::invokeMethod()</code> to call a slot if it has access to a receiving object but not a sending one to do a <code>connect()</code>.  Right?</p>
]]></description><link>https://forum.qt.io/post/825669</link><guid isPermaLink="true">https://forum.qt.io/post/825669</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 03 May 2025 12:04:04 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 11:55:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/825667">Invoking a slot from a non-Qt thread</a>:</p>
<blockquote>
<p dir="auto">Am I right, Christian?</p>
</blockquote>
<p dir="auto">Yes, but the question was if it can be invoked from a std::thread (or other non QThread based one). Nothing about what object it is 🙂</p>
]]></description><link>https://forum.qt.io/post/825668</link><guid isPermaLink="true">https://forum.qt.io/post/825668</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 03 May 2025 11:55:31 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 11:36:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/perdrix">@<bdi>Perdrix</bdi></a><br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> will correct me if I am wrong.  Yes, you can emit a signal from a non-QObject based thread.  But you still need a "context object" to emit the signal and pass to the <code>connect()</code>.  And that needs to be a <code>QObject</code>.  And you will use a queued connection.  I guess <code>QMetaObject::invokeMethod()</code> will skip the need to have an emitting <code>QObject</code>.</p>
<p dir="auto">You will also a need a receiving <code>QObject</code>, for both cases.  The Qt event loop needs to be running in the receiver thread to dispatch the slots.</p>
<p dir="auto">Am I right, Christian?</p>
<p dir="auto">References:<br />
<a href="https://stackoverflow.com/questions/41044526/qt-5-emit-signal-from-non-qt-thread" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/41044526/qt-5-emit-signal-from-non-qt-thread</a><br />
<a href="https://stackoverflow.com/questions/24982324/qt-emit-a-signal-from-a-c-thread" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/24982324/qt-emit-a-signal-from-a-c-thread</a><br />
<a href="https://www.qtcentre.org/threads/65029-Emitting-signal-from-a-non-Qt-Thread" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.qtcentre.org/threads/65029-Emitting-signal-from-a-non-Qt-Thread</a></p>
]]></description><link>https://forum.qt.io/post/825667</link><guid isPermaLink="true">https://forum.qt.io/post/825667</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 03 May 2025 11:36:56 GMT</pubDate></item><item><title><![CDATA[Reply to Invoking a slot from a non-Qt thread on Sat, 03 May 2025 11:17:17 GMT]]></title><description><![CDATA[<p dir="auto">Emit a signal.</p>
]]></description><link>https://forum.qt.io/post/825666</link><guid isPermaLink="true">https://forum.qt.io/post/825666</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 03 May 2025 11:17:17 GMT</pubDate></item></channel></rss>