博客
关于我
Qt安全使用线程
阅读量:240 次
发布时间:2019-03-01

本文共 653 字,大约阅读时间需要 2 分钟。

       使用背景:项目中需要开线程去播放声音、视频,简单的线程可以实现,Qt中简单的线程就是继承QThread类,然后重新run()方法即可,但是new出来的对象要时刻记得释放,不然就会有崩溃问题。于是想办法使用更为安全的线程,让系统替自己去管理new的对象,就是使用QRunnable类结合线程池实现。

       初始方法:

.h

class PlayThread : public QThread{public:    void run();};

.cpp

void PlayThread::run(){    my_exec("aplay test.wav");    this->deleteLater();}

      更改后得方法:

.h

class playThread : public QRunnable{public:    playThread(){}    explicit playThread(QString music){this->music = music;}    void run();private:    QString music {"test.wav"};}

.cpp

void playThread::run(){    my_exec(("aplay "+this->music));}

      使用方法和之前一样,包含头文件,在使用的类,私有成员中声明一个QThreadPool *playpool;然后再cpp中调用:playpool->start(new playThread);即可。

 

转载地址:http://vret.baihongyu.com/

你可能感兴趣的文章
mysql5.7的安装和Navicat的安装
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump备份时忽略某些表
查看>>
mysqlreport分析工具详解
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中null和空字符串的区别与问题!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>