Skip to content

Commit

Permalink
qt: Add ObjectInvoke template function
Browse files Browse the repository at this point in the history
This is a replacement of the QMetaObject::invokeMethod functor overload
which is available in Qt 5.10+.

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
  • Loading branch information
hebasto and ryanofsky committed Nov 25, 2020
1 parent afdfd3c commit 5659e73
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/qt/guiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ namespace GUIUtil
#endif
}

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

} // namespace GUIUtil

Expand Down

0 comments on commit 5659e73

Please sign in to comment.