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);
    });
    }

相关推荐
先吃饱再说10 小时前
存储的进化:从 MySQL 到浏览器缓存,数据到底住在哪?
数据库
Nturmoils10 小时前
字段太多看不全,ksql 的展开模式和输出控制怎么用
数据库·后端
Databend13 小时前
Agent 轨迹分析与归因的数据工程实践
大数据·数据库·agent
这个DBA有点耶13 小时前
SQL改写进阶:标量子查询的“隐形代价”与消除实战
数据库·mysql·架构
程序员老赵14 小时前
服务器文件不想 SFTP 上传?Docker 跑个 File Browser,浏览器就能管理
服务器·docker·开源
smallyoung14 小时前
数据库乐观锁深度解析:MySQL、PostgreSQL 实战 + Spring Boot 集成指南
数据库·mysql·postgresql
parade岁月14 小时前
MySQL JOIN解析:朴实无华但食之有味
数据库·后端
用户31693538118315 小时前
MySQL服务无法启动问题解决全记录
数据库
vivo互联网技术18 小时前
从 10 分钟到 1 秒:ES 深度分页任意跳页的三轮优化实战
服务器·数据库·redis·elasticsearch·深度分页