QMetaObject::invokeMethod
重载的文档:
template <
typename Functor,
typename FunctorReturnType>
bool QMetaObject::invokeMethod(
QObject *context,
Functor function,
Qt::ConnectionType type = Qt::AutoConnection,
FunctorReturnType *ret = nullptr)
是:
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.
我认为这意味着 lambda 或 std::function
可以作为参数 function
传递,因为它们都是仿函数类型。
我还假设参数 type
受到尊重,尽管文档中没有提及。
(“上下文
的事件循环”是什么意思?每个QObject
都以某种方式与事件循环相关联吗?术语“事件循环” of”指的是相关的事件循环)
调用函数
时会传递哪些参数(如果有)?
最佳答案
是的,您可以向
MetaObject::invokeMethod
传递 lambda 或 std::function。是的,
类型
受到尊重invokeMethod
向下调用QCoreApplication::postEvent
,其中receiver
是context
没有参数传递给
函数
(除非它是指向成员的指针,在这种情况下,接收者作为隐式对象参数传递) - 因此任何需要的参数都应该是 lambda-捕获或以其他方式绑定(bind)。
关于c++ - QMetaObject::invokeMethod 带有 lambda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65182030/