Discussion:
QMetaObject::invokeMethod
Robert Helling
2018-07-07 15:37:40 UTC
Permalink
Hi,

I am trying to understand some code that contains invocations of

QMetaObject::invokeMethod

I admit I not a C++ person in any sense so could somebody please explain why one would use such a thing? Berthold, I guess you introduced these. In particular since the Qt documentation says about QMetaObject

This class is not normally required for application programming, but it is useful if you write meta-applications, such as scripting engines or GUI builders.

Shouldn’t this rather be a signal that invokes a slot?

Thanks a lot
Robert
--
.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oO
Robert C. Helling Elite Master Course Theoretical and Mathematical Physics
Scientific Coordinator
Ludwig Maximilians Universitaet Muenchen, Dept. Physik
print "Just another Phone: +49 89 2180-4523 Theresienstr. 39, rm. B339
stupid .sig\n"; http://www.atdotde.de
Thiago Macieira
2018-07-07 16:54:58 UTC
Permalink
On Saturday, 7 July 2018 08:37:40 PDT Robert Helling wrote:
> Hi,
>
> I am trying to understand some code that contains invocations of
>
> QMetaObject::invokeMethod
>
> I admit I not a C++ person in any sense so could somebody please explain why
> one would use such a thing? Berthold, I guess you introduced these. In
> particular since the Qt documentation says about QMetaObject

Usually, that function is used with Qt::QueuedConnection, which instead of
calling the method indicated right now, it posts an event to the event queue
with the arguments you supply. When the event loop gets to that event, the
method you listed gets finally called.

Another way of doing the same is by using QTimer::singleShot() with a lambda
that carries your parameters. But this wasn't available before Qt 5.4, so
there's a lot of code (and muscle memory) using invokeMethod().

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
Robert Helling
2018-07-07 19:36:59 UTC
Permalink
Thiago,

> On 7. Jul 2018, at 18:54, Thiago Macieira <***@macieira.org> wrote:
>
> Usually, that function is used with Qt::QueuedConnection, which instead of
> calling the method indicated right now, it posts an event to the event queue
> with the arguments you supply. When the event loop gets to that event, the
> method you listed gets finally called.
>
> Another way of doing the same is by using QTimer::singleShot() with a lambda
> that carries your parameters. But this wasn't available before Qt 5.4, so
> there's a lot of code (and muscle memory) using invokeMethod().

but this doesn’t seem the application in the subsurface code. If you do

git grep invokeMethod

you will find the instances.

Best
Robert
Lubomir I. Ivanov
2018-07-07 19:45:47 UTC
Permalink
On 7 July 2018 at 22:36, Robert Helling <***@atdotde.de> wrote:
> Thiago,
>
> On 7. Jul 2018, at 18:54, Thiago Macieira <***@macieira.org> wrote:
>
> Usually, that function is used with Qt::QueuedConnection, which instead of
> calling the method indicated right now, it posts an event to the event queue
> with the arguments you supply. When the event loop gets to that event, the
> method you listed gets finally called.
>
> Another way of doing the same is by using QTimer::singleShot() with a lambda
> that carries your parameters. But this wasn't available before Qt 5.4, so
> there's a lot of code (and muscle memory) using invokeMethod().
>
>
> but this doesn’t seem the application in the subsurface code. If you do
>
> git grep invokeMethod
>
> you will find the instances.
>

the image related calls that don't have a connection type and are
technically using Qt::AutoConnection, which detects separate threads.
invokeMethod() can be also used to call functions in QML objects.

lubomir
--
Stöger, Berthold
2018-07-07 21:14:18 UTC
Permalink
Hi Robert,

Still abroad, therefore very briefly:

1) At the risk of being pedantic: This has nothing to do with C++, but with Qt/MOC. This wouldn't even work with plain C++. There's a slightly more modern pointer-to-member-function version, but that didn't compile on qt5.5.

2) I use this to call a function across thread boundaries (notably to transport data from a worker thread to the UI thread). For unknown reasons, QNetworkManager and QMediaPlayer refused to work in a non-UI thread.

3) You are right: This could be done just as well by sending a signal from the worker thread-object to a slot in the UI thread-object. In a PR I think Lubomir (sorry, if I mis-attribute) noted that connecting a signal from an object to itself is weird and therefore in this case invokeMethod was preferred. This just stuck, even if in this case it's connecting different objects.

4) I don't have any problems with this being changed to signal/slot, I would just ask to wait until the "Video-lite" PR is in master, so that we don't step on each other's toes.

Berthold
Loading...