QMetaObject::invokeMethod with a lambda?

Viewed 496

The documentation of this overload of QMetaObject::invokeMethod:

template <
   typename Functor,
   typename FunctorReturnType>
bool QMetaObject::invokeMethod(
   QObject *context,
   Functor function,
   Qt::ConnectionType type = Qt::AutoConnection,
   FunctorReturnType *ret = nullptr)

is:

Invokes the function in the event loop of context. function can be a functor or a pointer to a member function. Returns true if the function could be invoked. Returns false if there is no such function or the parameters did not match. The return value of the function call is placed in ret.

I assume this means a lambda or a std::function can be passed as as the argument function, as they are both functor types.

I also assume the argument type is respected, despite not being mentioned in the documentation.

(What does it mean by "the event loop of context"? Is every QObject associated with an event loop in some fashion? And the term "the event loop of" refers to that associated event loop)

What arguments (if any) are passed to function when it is invoked?

1 Answers
  • Yes, you can pass MetaObject::invokeMethod a lambda or std::function.

  • Yes, type is respected

  • invokeMethod calls down to QCoreApplication::postEvent where receiver is the context

  • No arguments are passed to function (unless its pointer-to-member, in which case the receiver is passed as the implicit object parameter) - so any needed arguments should be lambda-captured or otherwise bound.

Related