Skip to content

Commit 5659e73

Browse files
hebastoryanofsky
andcommitted
qt: Add ObjectInvoke template function
This is a replacement of the QMetaObject::invokeMethod functor overload which is available in Qt 5.10+. Co-authored-by: Russell Yanofsky <[email protected]>
1 parent afdfd3c commit 5659e73

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/qt/guiutil.h

+12
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,18 @@ namespace GUIUtil
340340
#endif
341341
}
342342

343+
/**
344+
* Queue a function to run in an object's event loop. This can be
345+
* replaced by a call to the QMetaObject::invokeMethod functor overload after Qt 5.10, but
346+
* for now use a QObject::connect for compatibility with older Qt versions, based on
347+
* https://stackoverflow.com/questions/21646467/how-to-execute-a-functor-or-a-lambda-in-a-given-thread-in-qt-gcd-style
348+
*/
349+
template <typename Fn>
350+
void ObjectInvoke(QObject* object, Fn&& function, Qt::ConnectionType connection = Qt::QueuedConnection)
351+
{
352+
QObject source;
353+
QObject::connect(&source, &QObject::destroyed, object, std::forward<Fn>(function), connection);
354+
}
343355

344356
} // namespace GUIUtil
345357

0 commit comments

Comments
 (0)