加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 连接lambda时如何将Qt :: ConnectionType传递给QObject ::

发布时间:2020-12-16 03:29:35 所属栏目:百科 来源:网络整理
导读:我将lambdas连接到QObject的信号: QObject::connect(handle,BatchHandle::progressMax,[this](const ProcessHandle* const self,const int value) { this-maxProgress(value); }); 上面的代码编译没有问题. 但是Qt :: QueuedConnection是绝对必要的,因为句
我将lambdas连接到QObject的信号:
QObject::connect(handle,&BatchHandle::progressMax,[this](const ProcessHandle* const self,const int value) {
        this->maxProgress(value);
    });

上面的代码编译没有问题.

但是Qt :: QueuedConnection是绝对必要的,因为句柄对象最终将移动到另一个线程.

我把它添加到我的代码中:

QObject::connect(handle,&BatchHandle::finished,[this](const ProcessHandle* const self) {
        this->processIsRunning(false);
    },(Qt::ConnectionType)Qt::QueuedConnection);

请注意我是如何添加显式强制转换以确保它正确标识值类型的.结果:

1>srcTechAdminServicesdatabasetechCoreprocessesimportImportManagerDialog.cpp(191): error C2664: 'QMetaObject::Connection QObject::connect<void(__cdecl taservices::ProcessHandle::* )(const taservices::ProcessHandle *),Qt::ConnectionType>(const taservices::ProcessHandle *,Func1,const QObject *,Func2,Qt::ConnectionType)' : cannot convert parameter 3 from 'taservices::`anonymous-namespace'::<lambda58>' to 'const QObject *'
1>          with
1>          [
1>              Func1=void (__cdecl taservices::ProcessHandle::* )(const taservices::ProcessHandle *),1>              Func2=Qt::ConnectionType
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion,or the operator cannot be called

连接lambda时如何获得排队连接?

解决方法

我认为你需要使用 QObject::connect overload,它允许你指定应该调用lambda的上下文…
QObject::connect(
  handle,target_context,/* Target context parameter. */
  [this](const ProcessHandle* const self,const int value)
  {
    this->maxProgress(value);
  },Qt::QueuedConnection);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读