当前位置:网站首页>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
}
边栏推荐
- 2022.08月--pushmall推贴共享电商更新与开发计划
- 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!.
- 指针和解引用
- 史上最全的Redis基础+进阶项目实战总结笔记
- 主流小程序框架性能分析
- tooltip control
- 【100个网络运维工作者必须知道的小知识!】
- 极化微波成像概述
- ROS2系列知识(7):用rqt_console查看日志logs
- 助推科技强国高质量发展《科创超级训练营》系列活动正式拉开帷幕
猜你喜欢
随机推荐
The anxiety of the post-90s was cured by the vegetable market
变量交换;复合赋值;增递减运算符
浅谈大数据背景下数据库安全保障体系
半自动化爬虫-爬取一个网站的内容及回复
千万级乘客排队系统重构&压测方案总结篇
今年最火爆的词:商业分析,看这一篇就够了!
研发团队数字化转型实践
When custom annotations implement log printing, specific fields are blocked from printing
TCP百万并发服务器优化调参
基于BiGRU和GAN的数据生成方法
02 es cluster construction
素域和扩域
金仓数据库KingbaseES安全指南--6.5. LDAP身份验证
云商店携手快报税,解锁财务服务新体验!
SQL的索引详细介绍
opencv实时人脸检测
Unity ui点击事件只响应最上层ui的方式
golang json 返回空值
参观首钢园
主流小程序框架性能分析









