Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMetaObject::invokeMethod alternative with compile-time checking

Besides QMetaObject::invokeMethod is there any type-safe way of invoking a method/slot asynchronously (a.k.a queuing its execution in the GUI thread)?

The QMetaObject::invokeMethod doesn't have compile-time checking for function names. There is also an overhead in specifying the member function by a string since a lookup and string matching is performed for every call.

I wonder if there is anything similar to the new QObject::connect syntax for invoking a method that provides compile time checking.

One solution is using the signal-slot architecture, but this enforces wrapping each caller code in a QObject class to be able to emit that signal.

like image 909
Isaac Avatar asked Nov 09 '22 10:11

Isaac


1 Answers

It is possible to use the QTimer::singleShot for this purpose

QTimer::singleShot(0, object, &Object::method...);
--
QTimer::singleShot(0, object, [object](){
  object->method(arg1, arg2, ...);
});

Note: The thread in which QTimer::singleShot is invoked must have an QEventLoop.

like image 64
thomas Avatar answered Nov 15 '22 06:11

thomas