当前位置:网站首页>Introduction to qqueue
Introduction to qqueue
2022-04-23 05:28:00 【Oriental forgetfulness】
QT The queue is a class QQueue, Its principle is just a QList Structure , In succession QListj On this basis, several functions are sealed .
template <class T>
class QQueue : public QList<T>
{
public:
// compiler-generated special member functions are fine!
inline void swap(QQueue<T> &other) Q_DECL_NOTHROW {
QList<T>::swap(other); } // prevent QList<->QQueue swaps
#ifndef Q_QDOC
// bring in QList::swap(int, int). We cannot say using QList<T>::swap,
// because we don't want to make swap(QList&) available.
inline void swap(int i, int j) {
QList<T>::swap(i, j); }
#endif
inline void enqueue(const T &t) {
QList<T>::append(t); }
inline T dequeue() {
return QList<T>::takeFirst(); }
inline T &head() {
return QList<T>::first(); }
inline const T &head() const {
return QList<T>::first(); }
};
swap(int i, int j)// To express oneself i Location and j Element exchange position of position .
swap(QQueue &other)// Indicates switching positions with other queues .
Common functions :
inline void enqueue(const T &t)// The team
inline T dequeue() // Out of the team .
Here is an example to see , How to use it .
#ifndef WIDGET_H
#define WIDGET_H
#include <QtWidgets>
#include <QThread>
#include <QQueue>
struct mydata
{
int id;
QString data;
};
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void slottest();
private:
QPushButton* m_btn;
};
class UThread : public QThread
{
public:
UThread();
void setExit(bool flag){
m_isExit = flag;}
void addQueueData(mydata& info);
void run();
private:
QQueue<mydata> m_queue;
bool m_isExit;
};
#endif // WIDGET_H
#include "widget.h"
#include "unistd.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
m_btn = new QPushButton(this);
QVBoxLayout* lay = new QVBoxLayout(this);
lay->addWidget(m_btn);
setLayout(lay);
connect(m_btn,SIGNAL(clicked()),this,SLOT(slottest()));
}
Widget::~Widget()
{
}
void Widget::slottest()
{
UThread* thread = new UThread;
thread->start();
int i = 0;
while (i < 30)
{
mydata info;
info.id = i;
info.data = "test";
thread->addQueueData(info);
i++;
}
}
/*----------thread-----------*/
UThread::UThread()
{
m_isExit = true;
}
void UThread::addQueueData(mydata &info)
{
m_queue.enqueue(info);// The team
}
void UThread::run()
{
while (m_isExit)
{
if (m_queue.size() > 0)
{
mydata info = m_queue.dequeue();// Out of the team
int id = info.id;
QString data = info.data;
qWarning()<<"id:"<<id<<"data:"<<data<<m_queue.size();
}
}
}
Print the results :
id: 0 data: "test" 29
id: 1 data: "test" 28
id: 2 data: "test" 27
id: 3 data: "test" 26
id: 4 data: "test" 25
id: 5 data: "test" 24
id: 6 data: "test" 23
id: 7 data: "test" 22
id: 8 data: "test" 21
id: 9 data: "test" 20
id: 10 data: "test" 19
id: 11 data: "test" 18
id: 12 data: "test" 17
id: 13 data: "test" 16
id: 14 data: "test" 15
id: 15 data: "test" 14
id: 16 data: "test" 13
id: 17 data: "test" 12
id: 18 data: "test" 11
id: 19 data: "test" 10
id: 20 data: "test" 9
id: 21 data: "test" 8
id: 22 data: "test" 7
id: 23 data: "test" 6
id: 24 data: "test" 5
id: 25 data: "test" 4
id: 26 data: "test" 3
id: 27 data: "test" 2
id: 28 data: "test" 1
id: 29 data: "test" 0
From the results, it is not difficult to see its entry and exit operation , fifo , last in last out .
版权声明
本文为[Oriental forgetfulness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220543307340.html
边栏推荐
- Understand the relationship between promise async await
- The introduction of lean management needs to achieve these nine points in advance
- CPT 104_TTL 09
- Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
- If I am PM's performance, movie VR ticket purchase display
- JS array common methods
- 可執行程序執行流程
- Basic knowledge of redis
- X86 assembly syntax: at & T and Intel
- Three of three JS (WEB GL) model deletion / scene emptying / simple sorting of memory release
猜你喜欢
Intel SGX preliminary learning and understanding notes (continuously updated)
phphphphphphphp
npm升级后问题,慌得一批
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
Fast application fuzzy search
Quick app bottom navigation bar
Branch and loop statements
Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
Five key technologies to improve the devsecops framework
Modèle axé sur le domaine DDD (III) - gestion des transactions à l'aide de Saga
随机推荐
WTL self drawn control library (cqscheckcomboxbox)
Three 之 three.js (webgl)旋转属性函数的简单整理,以及基于此实现绕轴旋转的简单案例
点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
狼叔来找翻译人员了--plato--持续翻译中.....
Log introduction and building web application
How to set the initial value of El input number to null
Laravel [view]
Intel SGX preliminary learning and understanding notes (continuously updated)
Anti crawler (0): are you still climbing naked with selenium? You're being watched! Crack webdriver anti crawler
Three of three JS (WEB GL) model deletion / scene emptying / simple sorting of memory release
Edit, cancel, pull up menu
Blender programmed terrain production
Double click The jar package cannot run the solution
JS array common methods
Escape characters \ splicing of data formats
varnish入门
Kanban Quick Start Guide
FileReader API file operation
Ehcache Memcache redis three caches
五一劳动节期间什么理财产品会有收益?