当前位置:网站首页>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
}
边栏推荐
猜你喜欢
随机推荐
开发工具:第五章:使用idea生成实体类
基于BiGRU和GAN的数据生成方法
块级元素、行内元素、行内块元素
实现mnist手写数字识别
tooltip control
[Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails
Unity ui点击事件只响应最上层ui的方式
SQL的ROUND函数用法及其实例
M1芯片电脑安装cerebro
2022年深圳市临床医学研究中心申请指南
ROS2系列知识(5):【参数】如何管理?
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
05 Doris cluster construction
数字化采购管理系统开发:精细化采购业务流程管理,赋能企业实现“阳光采购”
个人日记
夸克网盘资源站
C语言:表达式求值详解
金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(2. 概述)
opencv real-time face detection
SQL函数 TO_CHAR(一)









