当前位置:网站首页>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
}
边栏推荐
猜你喜欢
随机推荐
TCP million concurrent server optimization parameters
【R语言】线性混合模型进行重复测量设计分析
Live tonight!
半自动化爬虫-爬取一个网站的内容及回复
Basic image processing in opencv
SQL函数 TO_CHAR(三)
星途一直缺颠覆性产品?青岛工厂这款M38T,会是个突破点?
金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(2. 概述)
主流小程序框架性能分析
金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)
JumpServer堡垒机部署
【R语言】对图片进行裁剪 图片批量裁剪
金仓数据库KingbaseES安全指南--6.5. LDAP身份验证
频域分析实践介绍
Pytorch|GAN在手写数字集上的复现
QT_事件类
GTK修改pixmap像素,提取pixmap像素RGB值
2022年SQL经典面试题总结(带解析)
08 Spark cluster construction
关系运算符和if,else语句