QT:正确延时调用,Cannot create children for a parent that is in a different thread.

试图使用线程、定时器,延时执行,会发现十分的麻烦。

这是因为现在代码都涉及到线程,比如主线程中禁止什么允许什么。

如何在QT中延时执行一段代码?

  • 被动调用。比如在resize()中,可以直接使用定时器:

    复制代码
      video_timer = new QTimer(this);
      connect(video_timer, &QTimer::timeout, this, &VideoWidget::onTimeout);
      video_timer->start(1000);
  • 主动调用。比如收到消息,执行操作:

    //第一步
    QMetaObject::invokeMethod(this, "onReplay", Qt::QueuedConnection);

    //第二步
    void VideoWidget::onReplay()
    {
    qDebug() << "================== onReplay";
    QTimer::singleShot(3000, this {
    QMetaObject::invokeMethod(this, "playAction", Qt::QueuedConnection);
    });
    }

相关推荐
拾光Ծ1 小时前
【MySQL】对表数据的操作:增删查改(CRUD)
android·数据库·sql·mysql
程与留14 小时前
15_国际化和本地化:tr()、ts 文件、QM 文件、多语言切换
c++·qt
这个DBA有点耶14 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba
程与留15 小时前
14_Qt 样式表(QSS)入门(语法、选择器、美化实战)
c++·qt
这个DBA有点耶17 小时前
从MySQL 5.7到8.0:JSON查询性能差在哪?虚拟列索引vs多值索引怎么选
数据库·mysql·架构
胡写代码17 小时前
数据库审计字段,别再每张表各写一套了——我统一成这 6 个字段
数据库
SelectDB21 小时前
为什么 JSON 正在成为分析数据库新的竞争点?
数据库·json·agent
ClouGence1 天前
开源数据库管理工具 CloudDM 4.2.0 发布,新增 GoldenDB、KingbaseES 等数据源
数据库·dba·devops
发霉的馒头1 天前
ORA-00845: MEMORY_TARGET not supported on this system的解决方法
数据库
Quz1 天前
QML 拖拽篇:接收拖拽与数据交换
qt