c++ - QMetaObject::invokeMethod 带有 lambda?

标签 c++ qt

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,其中 receivercontext

  • 没有参数传递给函数(除非它是指向成员的指针,在这种情况下,接收者作为隐式对象参数传递) - 因此任何需要的参数都应该是 lambda-捕获或以其他方式绑定(bind)。

关于c++ - QMetaObject::invokeMethod 带有 lambda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65182030/

相关文章:

java - 有什么办法可以知道C++模板中的函数是什么?

c++ - 我的模板类返回了错误的数据

c++ - QTime->addSecs 导致段错误

c++ - 在qt中动态添加小部件

c++ - QRegExp : search a pattern that does not contain another pattern

c++ - MySQL服务器消失了

c++ - Visual Studio 中的 M_PI 问题

c++ - 如何在 visual studio 2015 构建结束时复制文件

c++ - Qt 将 base64 转换为 QImage 返回 null

c++ - 将 NULL/0 传递给 dynamic_cast 的便携安全?