当前位置:网站首页>QT_QThread线程
QT_QThread线程
2022-08-01 17:33:00 【小小工程员】
QT_QThread线程

注意的是:在线程中不能调用槽函数,否则会出现意想不到的错误,如调用w.show等!可以发出信号让某个槽函数响应,即尽量让线程独立,解耦合。
设计对窗口的操作,都要放到槽中去做。在线程中不要做
头文件
#pragma once
#include <QThread>
#include <QMutex>
#include "opencv2/core.hpp" // Mat所在的头文件
class XVideoThread :public QThread //单件模式,不要拿这个类去创建对象,只做接口调用。
{
Q_OBJECT // 信号与槽才会有效
public:
//单件模式 获取对象
static XVideoThread* Get() //通过一个静态函数;
{
static XVideoThread vt; //这个对象只生成一个,称之为单件模式; 只能通过这个get方式获取对象,进行调用,保证 对象具有唯一性。
return &vt; //返回指针类型
}
void Play() {
mutex.lock(); isPlay = true; mutex.unlock(); }; //打开视频
void Pause() {
mutex.lock(); isPlay = false; mutex.unlock(); };
~XVideoThread();
//线程入口函数
void run();
signals:
protected:
QMutex mutex; //线程互斥
XVideoThread(); //构造函数放在保护中,在外部就无法用这个类去生成对象。
};
/* 如何使用这种单件模式? 接口调用方法: XVideoThread::Get()获取这个对象vt,再通过指针调用这个对象的成员函数Open。 if (!XVideoThread::Get()->Open(file)) { QMessageBox::information(this, "error", name+" open failed!"); return; } */
源文件
#include "XVideoThread.h"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include "XFilter.h"
using namespace std;
using namespace cv;
void XVideoThread::run() //注意的是,当线程被销毁的时候,这个线程有可能没被销毁,导致关闭程序时显示程序被shutdown掉。
{
for (;;)
{
mutex.lock(); //锁的原则,尽晚调用,尽早退出
if (isexit)
{
mutex.unlock(); //尽早退出,一定不能遗漏
break;
}
if (!isPlay) //如果处于暂停
{
mutex.unlock();
msleep(5);
continue;
}
msleep(s); //根据fps设定帧间隔
mutex.unlock(); //一定记得各个条件下都要退出
}
}
XVideoThread::XVideoThread()
{
start(); //线程启动。XVideoThread继承public QThread 这个类,XVideoThread中重载了void run();函数,则调用start()后,则会创建一个线程,并把run()函数变成入口函数
}
XVideoThread::~XVideoThread()
{
// terminate(); //粗暴的终止线程。
mutex.lock(); //线程先锁住。
isexit = true; //等待一次run()函数中for循环完毕后,将isexit标志位置为真,run()函数结束。 相当于线程退出操作quit(); 。
mutex.unlock(); //线程解掉
wait(); //等待线程的退出!!! 必须在这里等待线程执行完退出
}
边栏推荐
- GridControl helper class for DevExpress
- 金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)
- 云商店携手快报税,解锁财务服务新体验!
- How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
- 酷逼了 Pathetic Dog 第 304 场周赛
- 金仓数据库KingbaseES安全指南--6.3. Kerberos身份验证
- 金仓数据库KingbaseES安全指南--6.9. Ident身份验证
- 极化微波成像概述2
- The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
- 生物制药产业发展现状和趋势展望
猜你喜欢
随机推荐
移动端吸顶方案
hcip第九天
C # Excel helper classes
想做期货,农产品期货怎么炒?波动大么
Unity ui点击事件只响应最上层ui的方式
[供应链·案例篇]石油和天然气行业的数字化转型用例
表达式;运算符,算子;取余计算;运算符优先顺序
面经汇总-社招-6年
The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
后台管理系统的权限思路
GridControl helper class for DevExpress
素域和扩域
请问数据库中报错信息如下,mongoshake 有什么配置的方式解决这种大消息问题吗?
DataTable Helper Class for C#
理财产品的月年化收益率怎么算?
Complete knapsack problem to find the number of combinations and permutations
2022年SQL经典面试题总结(带解析)
SQL的ROUND函数用法及其实例
SQL的索引详细介绍
金仓数据库 KDTS 迁移工具使用指南(2. 简介)









