当前位置:网站首页>QT_QThread thread
QT_QThread thread
2022-08-01 17:38:00 【little engineer】
QT_QThread线程

注意的是:Slot functions cannot be called from within a thread,否则会出现意想不到的错误,如调用w.show等!It is possible to signal a slot function to respond,That is, try to make threads independent,解耦合.
Design operations on windows,Put it in the tank to do it.Don't do it in a thread
头文件
#pragma once
#include <QThread>
#include <QMutex>
#include "opencv2/core.hpp" // Mat所在的头文件
class XVideoThread :public QThread //单件模式,Don't use this class to create objects,Only make interface calls.
{
Q_OBJECT // Signals and slots are only valid
public:
//单件模式 获取对象
static XVideoThread* Get() //via a static function;
{
static XVideoThread vt; //This object only generates one,Call it the singleton pattern; 只能通过这个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(); //Constructors are placed in guards,You cannot use this class to generate objects externally.
};
/* How to use this singleton pattern? 接口调用方法: XVideoThread::Get()获取这个对象vt,Then call the member function of this object through the pointerOpen. 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() //注意的是,当线程被销毁的时候,It is possible that this thread was not destroyed,Causes the program to be displayed when the program is closedshutdown掉.
{
for (;;)
{
mutex.lock(); //lock principle,Call as late as possible,尽早退出
if (isexit)
{
mutex.unlock(); //尽早退出,一定不能遗漏
break;
}
if (!isPlay) //If it is on hold
{
mutex.unlock();
msleep(5);
continue;
}
msleep(s); //根据fpsSet the frame interval
mutex.unlock(); //Always remember to exit under all conditions
}
}
XVideoThread::XVideoThread()
{
start(); //线程启动.XVideoThread继承public QThread 这个类,XVideoThread中重载了void run();函数,则调用start()后,A thread will be created,并把run()The function becomes the entry function
}
XVideoThread::~XVideoThread()
{
// terminate(); //Terminate the thread violently.
mutex.lock(); //The thread is locked first.
isexit = true; //等待一次run()函数中for循环完毕后,将isexitThe flag position is true,run()函数结束. Equivalent to the thread exit operationquit(); .
mutex.unlock(); //Thread unraveled
wait(); //等待线程的退出!!! You must wait here for the thread to finish executing and exit
}
边栏推荐
- 【TDP加码福利】COS用户实践征文月,等你来投稿!!!
- Isometric graph neural networks shine in drug discovery
- 金仓数据库KingbaseES安全指南--6.5. LDAP身份验证
- 块级元素、行内元素、行内块元素
- 直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
- QT基础功能,信号、槽
- golang json 返回空值
- OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
- ROS2系列知识(7):用rqt_console查看日志logs
- MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
猜你喜欢
随机推荐
SQL窗口函数
C语言理论--笔试面试基础稳固
金仓数据库 KDTS 迁移工具使用指南(2. 简介)
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
个人日记
移动端吸顶方案
缓存一致性MESI与内存屏障
网上开户佣金万一靠谱吗,网上开户安全吗
存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
参观首钢园
md5sum源码 可多平台编译
Flask框架实战
极化微波成像概述2
opencv实时人脸检测
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
2022.08月--pushmall推贴共享电商更新与开发计划
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
ROS2系列知识(7):用rqt_console查看日志logs
我在启牛开户安全吗?谁能告诉我开不靠谱?
The anxiety of the post-90s was cured by the vegetable market









