当前位置:网站首页>QT -- thread
QT -- thread
2022-07-05 23:55:00 【I have a mint candy】
Tips : This is the learning record , If there is an error , Please contact the author , Modest and educated .
List of articles
Preface
Find a partner like the sun , It's worth nothing to help you dry everything .
One 、QT Operation thread
1、 By inheritance QThread class , rewrite QThread Class run function , control Run Function to operate a new thread .
2、 By inheritance QObject class , Write the thread operation function in the new class , adopt movetothread() Function to transfer the newly created class to a new thread .
Two 、 Inherit QThread, rewrite run()
1.mythread class
mythread.h file
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QDebug>
class MyThread : public QThread
{
public:
explicit MyThread(){
}
private:
void run() override;
private slots:
void sig_ShowMsg(QString,bool);
#endif // MYTHREAD_H
mythread.cpp file
void MyThread::run()
{
// Execution of complex functions
emit sig_ShowMsg(" Send successfully ",true);
}
2.mainwindow
mainwindow.h file
// Define the thread
MyThread* m_thread;
signals:
void slt_ShowMsg(QString ,bool );
mainwindow.cpp file
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Declaration thread
m_thread = new MyThread;
// The two are connected with the slot through signals
connect(&m_thread,SIGNAL(sig_ShowMsg(QString,bool)),SLOT(slt_ShowMsg(QString ,bool )));
}
// Trigger the thread to start through the button
void MainWindow::on_btnstart_clicked()
{
thread->start();// When using this function, it will automatically call run function
qDebug()<<"main thread: "<< QThread::currentThreadId();
emit sig_start();
}
// Stop the thread through the button
void MainWindow::on_btnclose_clicked()
{
// End thread
thread->exit();
thread->deleteLater();
emit sig_Close();
}
void MainWindoe::slt_ShowMsg(QString a,bool b)
{
ui->text->setText("a");
}
The connection between the main thread and the sub thread is through the signal and slot .
Tips : Only in Run The content of the function is in the new thread , Other functions are in old threads .
3、 ... and 、 Inherit QObject, adopt moveToThread()
1.MyObject class
// Thread processing object
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QThread>
#include <QDebug>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = 0): QObject(parent){
}
~MyObject(){
}
private slots:
void Start()// Thread handler
{
qDebug()<<"obj thread: "<<QThread::currentThreadId();
}
};
#endif // MYOBJECT_H
stay Mainwindow When used in ,
// Defining variables
MyObject* obj;
QThread* thread;
// Start thread
obj = new MyObject;// Thread execution object
thread = new QThread;
obj->moveToThread(thread);// Move the object to the newly created thread
connect(thread, &QThread::started, obj, &MyObject::Start);// Bind the start signal of the thread , After the thread starts, it will execute obj Of Start Slot function
thread->start();
qDebug()<<"main thread: "<<QThread::currentThreadId();
// End thread
thread->exit();
thread->deleteLater();// Delete object
obj->deleteLater();
Use QObject Methods
- Write an inheritance QObject Class , Declare the entry function requiring complex and time-consuming logic as slot function .
- This class is in the old thread new come out , You can't set any parent to it
- Also declare a QThread, If not new, When destructing, you need to call QThread::wait(), If it's heap allocation , Can pass deleteLater Let the thread destroy automatically .
- hold obj adopt moveToThread Method to the new thread , here object Already in the thread .
- Put the thread of finished Signals and object Of deleteLater Slot connection , This signal slot must be connected to , Otherwise, there will be a memory leak .
- Connect other signals and slots normally , Communicate through signals and slots .
- After the initialization, it is called QThread::start() To start a thread
- At the end of the logic , call QThread::quit Exit the thread's event loop
Use QObject To implement multithreading rather than inheritance QThread The method is more flexible , The whole class is in the new thread , Pass data through the signal slot and the main thread
Four 、 Destruction of the thread
connect(&m_thread,&QThread::finished ,&obj,&QObject::deleteLater);
Make a statement QThread, If not new, When destructing, you need to call QThread::wait(),
If it's heap allocation , Can pass deleteLater Let the thread destroy automatically .
( The heap needs to be destroyed manually by the programmer )
Insert :
Decimal display :
QString test = QString::number(rcvBuf3[5]*256 +rcvBuf3[6]);
QString data = QString::number(rcvBuf3[5]<<8 | rcvBuf3[6]);
summary
Good at summarizing , More further .
边栏推荐
- Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
- What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
- Do you regret becoming a programmer?
- Which side projects can be achieved? Is it difficult for we media to earn more than 10000 a month?
- My colleagues quietly told me that flying Book notification can still play like this
- Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
- What are Yunna's fixed asset management systems?
- Huawei simulator ENSP - hcip - MPLS experiment
- 【LeetCode】5. Valid palindrome
- Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
猜你喜欢

Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布

多普勒效应(多普勒频移)

保研笔记四 软件工程与计算卷二(8-12章)

云呐|固定资产管理系统主要操作流程有哪些

零犀科技携手集智俱乐部:“因果派”论坛成功举办,“因果革命”带来下一代可信AI

Transport layer protocol ----- UDP protocol

妙才周刊 - 8

Go language introduction detailed tutorial (I): go language in the era

4 points tell you the advantages of the combination of real-time chat and chat robots

Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste
随机推荐
Cwaitabletimer timer, used to create timer object access
XML configuration file (DTD detailed explanation)
哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
mysql-全局锁和表锁
有什么不起眼却挣钱的副业?
保研笔记一 软件工程与计算卷二(1-7章)
多普勒效应(多普勒频移)
Problem solving win10 quickly open ipynb file
Yunna | what are the main operating processes of the fixed assets management system
Cloudcompare & PCL point cloud randomly adds noise
Huawei simulator ENSP - hcip - MPLS experiment
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Qt QPushButton详解
Spire Office 7.5.4 for NET
Miaochai Weekly - 8
shardingsphere源码解析
PADS ROUTER 使用技巧小记
China Jinmao online electronic signature, accelerating the digitization of real estate business
14 MySQL view
18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)