<?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[Relevance of invokeMethod() in multithreaded programs]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a><br />
Thanks.  I think I understand this.  I had been about to post to ask what you had meant by</p>
<blockquote>
<p dir="auto">Yes, invokeMethod() is also my general solution if I'm multithreaded.</p>
</blockquote>
<p dir="auto">I didn't see why there is a particular mention here of separate threads.  Still not sure why there is?  Is there anything in what you have written which makes this especially apposite to cross-thread rather than single-thread?  Your point about avoiding many signals/slots, but why is the multithread relevant?</p>
<p dir="auto">It <em>sounds</em> to me as though <code>invokeMethod()</code> is just sort of the heart of the older call-slot-by-name <code>SLOT()</code> code approach?</p>
<p dir="auto">Having heard of but never used it, I had <em>thought</em> it did some kind of "invoke method on object <em>from thread in which object lives</em>", context switching for you if not current thread.  So you didn't have to worry about threads being different.  Ah, I guess it does if you pass it <code>Qt::AutoConnection</code>, just like signal <code>connect()</code>s.  But it doesn't do it there and then, it uses the usual Qt delayed queuing if across threads.  I had thought it was doing something more immediate.</p>
<p dir="auto">OK, so since it looks like it's a quick fire-slot-as-though-connected, why do you find it particularly suitable in multithreaded?</p>
]]></description><link>https://forum.qt.io/topic/164895/relevance-of-invokemethod-in-multithreaded-programs</link><generator>RSS for Node</generator><lastBuildDate>Thu, 03 Sep 2026 11:23:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164895.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Jul 2026 06:55:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Relevance of invokeMethod() in multithreaded programs on Mon, 20 Jul 2026 07:39:51 GMT]]></title><description><![CDATA[<p dir="auto">Basically, the other answers already contain all of my thoughts. But, since I've been mentioned I'll still chime in.</p>
<p dir="auto">In a single-threaded context I can just call functions and that's fine. In a multi-threaded context when I want to call functions of an object that lives in a different thread (especially if you want to call GUI functions in Qt) invokeMethod() is the only easy way I know of. Sure, you can properly set up a signal. I personally don't see the point in having a signal (which I then need to connect) if it is called from just a single place in the code. First, I have to come up with an appropriate name for the signal, and second, I'll have a long list of signals in the class declaration that are of no interest to any outsider.</p>
<p dir="auto">The use case of of using this to call functions belonging to a GUI thread is so pervasive that I have a header-only library that (among other things) has a function <code>guiThread(...)</code> (<a href="https://github.com/SimonSchroeder/QtThreadHelper" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/SimonSchroeder/QtThreadHelper</a>) to easily place calls into the GUI thread from other threads. From the recent discussion in this forum I have also learned that invokeMethod() takes a connection type as argument. I guess, then my function <code>guiThreadMaybe()</code> is not necessary, as the default is the AutoConnection which will do already a direct call if it is from the same thread (the 'maybe' part explicitly checks for that).</p>
<p dir="auto">Outside of multi-threading I only see a single use case for invokeMethod(): If I don't want to immediately execute that function, but put it in the event queue to be executed at a later point. I guess this is a valid use case, but not one I encounter often. In many cases, the suggested solution is a single shot timer with a timeout of 0ms. In addition to putting this call into the event loop, it will also only execute once the event loop is otherwise idle.</p>
<hr />
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/839195">Relevance of invokeMethod() in multithreaded programs</a>:</p>
<blockquote>
<p dir="auto">I didn't see why there is a particular mention here of separate threads.</p>
</blockquote>
<p dir="auto">It wasn't mentioned in the original post, but later:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said in <a href="/post/839167">Show a QMessage box from the context of a function defined outside mainwindow.cpp</a>:</p>
<blockquote>
<p dir="auto">One problem with a simple callback will arise when you run the solver in a different thread.</p>
</blockquote>
<p dir="auto">"invokeMethod() is also my general solution if I'm multithreaded" needs to be read in the context of the original discussion: It was specifically about calling GUI functions. If we combine that with multithreading, invokeMethod() is the (hard-to-find) obvious solution.</p>
]]></description><link>https://forum.qt.io/post/839234</link><guid isPermaLink="true">https://forum.qt.io/post/839234</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 20 Jul 2026 07:39:51 GMT</pubDate></item><item><title><![CDATA[Reply to Relevance of invokeMethod() in multithreaded programs on Sat, 18 Jul 2026 14:38:21 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/839200">Relevance of invokeMethod() in multithreaded programs</a>:</p>
<blockquote>
<ul>
<li>It just seems to do the same as if you had set up a connect(), but saves you having to do that, not really anything else?</li>
</ul>
<p dir="auto">Particularly from the last point, I then wonder why Simon says this is especially useful in a multithreaded context?</p>
</blockquote>
<p dir="auto">Have you had a closer look at my threaded QMessageBox example? (Note: I don't generally encourage blocking connections, but there are niche use-cases for it; it's a demonstration of what's possible)</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/839206">Relevance of invokeMethod() in multithreaded programs</a>:</p>
<blockquote>
<p dir="auto">the example of</p>
<pre><code>// populate myList
QMetaObject::invokeMethod(m_model, [=] { m_model-&gt;setNewMyList(myList); });
</code></pre>
<p dir="auto">just looks to me like I natural example of emitting a signal for "here is some new data for the model", so I don't really see why it's important to have that using <code>invokeMethod()</code>?</p>
</blockquote>
<p dir="auto">What if you DON'T have a signal? What if <code>myList</code> isn't even held by a QObject at all?</p>
]]></description><link>https://forum.qt.io/post/839211</link><guid isPermaLink="true">https://forum.qt.io/post/839211</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Sat, 18 Jul 2026 14:38:21 GMT</pubDate></item><item><title><![CDATA[Reply to Relevance of invokeMethod() in multithreaded programs on Fri, 17 Jul 2026 19:10:01 GMT]]></title><description><![CDATA[<p dir="auto">Hmm.  As I say, I have never used it, though seen others doing so.  My usage of Qt may be different from you all.  I did understand <a class="plugin-mentions-user plugin-mentions-a" href="/user/grecko">@<bdi>GrecKo</bdi></a>'s post.  But the example of</p>
<pre><code>// populate myList
QMetaObject::invokeMethod(m_model, [=] { m_model-&gt;setNewMyList(myList); });
</code></pre>
<p dir="auto">just looks to me like I natural example of emitting a signal for "here is some new data for the model", so I don't really see why it's important to have that using <code>invokeMethod()</code>?</p>
]]></description><link>https://forum.qt.io/post/839206</link><guid isPermaLink="true">https://forum.qt.io/post/839206</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 17 Jul 2026 19:10:01 GMT</pubDate></item><item><title><![CDATA[Reply to Relevance of invokeMethod() in multithreaded programs on Fri, 17 Jul 2026 18:12:23 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">To add to my fellows answers, in the case of a callback, it allows to call into Qt's meta object without requiring complex plumbing.<br />
If memory serves well, I used to do that with some camera SDK's that provided callback running in different threads to update the GUI in a straightforward fashion.</p>
]]></description><link>https://forum.qt.io/post/839205</link><guid isPermaLink="true">https://forum.qt.io/post/839205</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 17 Jul 2026 18:12:23 GMT</pubDate></item><item><title><![CDATA[Reply to Relevance of invokeMethod() in multithreaded programs on Fri, 17 Jul 2026 12:08:17 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">The docs state "Invokes the member (a signal or a slot name) on the object obj.". That implies to me you need to have marked the desired member method as a slot. Maybe it does not matter with slots...?</p>
</blockquote>
<p dir="auto">The doc states that for a subset of the <code>invokeMethod</code> overloads (with a <code>const chat *member</code> second argument). Other states "Invokes the function [...] in the event loop of context. function can be a functor or a pointer to a member function."</p>
<p dir="auto">Those don't need to be marked as slot.</p>
<blockquote>
<p dir="auto">The docs state "With asynchronous method invocations, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. ". So just like signals/slots you need support for each data type?</p>
</blockquote>
<p dir="auto">Yes but nowadays the meta-object system is mostly automatic and you don't need explicit registration.</p>
<p dir="auto">3rd point is moot given the overloads taking a functor or member-function pointer.</p>
<blockquote>
<p dir="auto">It just seems to do the same as if you had set up a connect(), but saves you having to do that, not really anything else?</p>
</blockquote>
<p dir="auto"><code>invokeMethod</code> saves you the unnecessary plumbing and leaking (eh) your internal implementation when there is already an existing coupling between the caller and the callee.<br />
When I want to call a known function in another thread, that's what I use. Signals are for notifying unknown (to the caller) external objects of an event they may be interested in.</p>
<p dir="auto">One of my common usage is modifying GUI stuff in the GUI thread from a background thread. For example I have a background thread compiling a list of data to be presented in a view via a model:</p>
<pre><code>// populate myList
QMetaObject::invokeMethod(m_model, [=] { m_model-&gt;setNewMyList(myList); });
</code></pre>
]]></description><link>https://forum.qt.io/post/839202</link><guid isPermaLink="true">https://forum.qt.io/post/839202</guid><dc:creator><![CDATA[GrecKo]]></dc:creator><pubDate>Fri, 17 Jul 2026 12:08:17 GMT</pubDate></item><item><title><![CDATA[Reply to Relevance of invokeMethod() in multithreaded programs on Fri, 17 Jul 2026 09:00:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> said in <a href="/post/839197">Relevance of invokeMethod() in multithreaded programs</a>:</p>
<blockquote>
<p dir="auto">You can run ANY function in any thread of your choice that has an active event loop:</p>
</blockquote>
<ul>
<li>
<p dir="auto">The docs state "Invokes the member (a signal or a slot name) on the object obj.".  That <em>implies</em> to me you need to have marked the desired member method as a slot.  Maybe it does not matter with slots...?</p>
</li>
<li>
<p dir="auto">The docs state "With asynchronous method invocations, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. ".  So just like signals/slots you need support for each data type?</p>
</li>
<li>
<p dir="auto">You are using a runtime lookup of member function name and parameter numbers and types.  So you lose the compile-time checking of parameters/signatures in modern <code>connect()</code>.  It's even slightly worse than the old <code>SIGNAL()/SLOT()</code> mechanism, didn't that check signatures of each for compatibility?</p>
</li>
<li>
<p dir="auto">It just seems to do the same as if you had set up a <code>connect()</code>, but saves you having to do that, not really anything else?</p>
</li>
</ul>
<p dir="auto">Particularly from the last point, I then wonder why Simon says this is especially useful in a multithreaded context?  Thinking aloud, perhaps when you want to call a function within same thread you can often just call it directly without any kind of signal/slot/connect()/invokeMethod() but that is not so easy if the function is in another thread so invokeMethod() is a convenient way if you have not gone signal/slot/connect() approach?</p>
<p dir="auto">I am not trying to be difficult.  I am just trying to understand <code>invokeMethod()</code> and why/when you would use it over <code>connect()</code>s.  And why it is regarded as especially useful when multithreaded.</p>
]]></description><link>https://forum.qt.io/post/839200</link><guid isPermaLink="true">https://forum.qt.io/post/839200</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 17 Jul 2026 09:00:33 GMT</pubDate></item><item><title><![CDATA[Reply to Relevance of invokeMethod() in multithreaded programs on Fri, 17 Jul 2026 08:17:45 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/839195">Show a QMessage box from the context of a function defined outside mainwindow.cpp</a>:</p>
<blockquote>
<p dir="auto">why do you find it particularly suitable in multithreaded?</p>
</blockquote>
<p dir="auto">You can run ANY function in any thread of your choice that has an active event loop:</p>
<pre><code>auto o1 = new QObject;
o1-&gt;moveToThread(thread1);

auto o2 = new QObject;
o2-&gt;moveToThread(thread2);

...

foo(); // Run foo() in the current thread

QMetaObject::invokeMethod(o1, []{
    foo(); // Run foo() in thread1
});
QMetaObject::invokeMethod(o2, []{
    foo(); // Run foo() in thread2
});
QMetaObject::invokeMethod(qApp, []{
    foo(); // Run foo() in the GUI thread
});
</code></pre>
<p dir="auto">Lambdas + <code>invokeMethod()</code> also provides another easy way to pass data between threads:</p>
<pre><code>bool stopProcessing = false;

QMetaObject::invokeMethod(qApp, [&amp;stopProcessing]{
    // Show message box in the GUI thread
    auto btn = QMessageBox::question(nullptr, "Uh-oh", "Houston, we have a problem. Permission to abort mission?");

    // Return data to the secondary thread
    stopProcessing = (btn == QMessageBox::Yes);

}, Qt::BlockingQueuedConnection); // Block the secondary thread until the lambda returns

// Read and use the data in secondary thread
if (stopProcessing) {
    // I'm free!
} else {
    // Save me...
}
</code></pre>
]]></description><link>https://forum.qt.io/post/839197</link><guid isPermaLink="true">https://forum.qt.io/post/839197</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Fri, 17 Jul 2026 08:17:45 GMT</pubDate></item></channel></rss>