欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Qt中線程的使用方法

 更新時間:2022年12月16日 08:16:57   作者:音視頻開發(fā)老舅  
這篇文章主要為大家詳細介紹了Qt中線程的使用方法,文中的示例代碼講解詳細,對我們學習Qt有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下

QT中使用線程可以提高工作效率。

要使用線程要經過一下四個步驟:

(1)先創(chuàng)建一個c++ class文件,記得繼承Thread,創(chuàng)建步驟如下:

a、第一步

b、第二步

(2)自定義一個run函數(shù),以后啟動線程的時候,程序就會跳轉到run函數(shù)中

void run();

(3)初始化線程

HDThread mythread = new HDThread();

(4)啟動線程

mythread->start();

下面來看看線程使用的具體列子:

線程頭文件hdthread.h:

#ifndef HDTHREAD_H
#define HDTHREAD_H
#include <QThread>
#include <QLabel>
#include <QMutex>

class HDTHread : public QThread
{
public:
    HDTHread(QMutex* mtex,QObject *parent = 0);
    void run();//自定義的run函數(shù)
    void setLabel(QLabel *lb);
private:
    QLabel *label;
    QMutex *mutex; //互斥鎖
};

#endif // HDTHREAD_H

主函數(shù)的頭文件threadqt.h

#ifndef THREADQT_H
#define THREADQT_H

#include <QMainWindow>
#include <hdthread.h>
#include <writefile.h>
#include <QMutex>
#include <QSemaphore>

namespace Ui {
class ThreadQt;
}

class ThreadQt : public QMainWindow
{
    Q_OBJECT

public:
    explicit ThreadQt(QWidget *parent = 0);
    ~ThreadQt();

     //定義靜態(tài)的信號類
    static QSemaphore *sp_A;
    static QSemaphore *sp_B;
private slots:
    void on_pushButton_clicked();

private:
    Ui::ThreadQt *ui;

    HDTHread *thread; //hdtread類,里面繼承了線程
    WriteFile *writethread;
    QMutex mutex;//定義互斥鎖類

};

#endif // THREADQT_H

源文件hdthread.cpp:

#include "hdthread.h"
#include <QDebug>
#include <threadqt.h>
HDTHread::HDTHread(QMutex *mtex, QObject *parent):QThread(parent)//構造函數(shù),用來初始化
{
    this->mutex = mtex;
}
void HDTHread::setLabel(QLabel *lb)
{
    this->label = lb;
}
 
void HDTHread::run() //啟動線程時執(zhí)行的函數(shù)
{
    while(true)
    {
 
        qint64 data = qrand()%1000; //取隨機數(shù)
        //this->mutex->lock();//上鎖
        ThreadQt::sp_A->acquire();//請求信號
        this->label->setText(QString::number(data));
         sleep(1);
        ThreadQt::sp_B->release();//釋放信號
        //this->mutex->unlock();//解鎖
 
        qDebug()<<"hello Qt"<<data;
    }
}

源文件threadqt.cpp

#include "threadqt.h"
#include "ui_threadqt.h"
 
//初始化靜態(tài)變量
 QSemaphore *ThreadQt::sp_A = NULL;
 QSemaphore *ThreadQt::sp_B = NULL;
 
ThreadQt::ThreadQt(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ThreadQt)
{
    ui->setupUi(this);
    //創(chuàng)建信號對象
    sp_A = new QSemaphore(1);
    sp_B = new QSemaphore(0);
 
}
 
ThreadQt::~ThreadQt()
{
    delete ui;
}
 
void ThreadQt::on_pushButton_clicked()
{
    thread = new HDTHread(&mutex); //初始化線程
    thread->setLabel(ui->label);
    thread->start();//開啟線程
 
    writethread = new WriteFile(&mutex);
    writethread->setLabel(ui->label);
    writethread->start();
}

大家也看到了,此處的線程也用到了互斥鎖(信號量)保證線程讀寫數(shù)據(jù)時不出現(xiàn)錯誤,這里大家可以看一下具體實現(xiàn)的代碼

this->mutex->lock();//上鎖
ThreadQt::sp_A->acquire();//請求信號
this->label->setText(QString::number(data));
sleep(1);
ThreadQt::sp_B->release();//釋放信號
this->mutex->unlock();//解鎖

到此這篇關于詳解Qt中線程的使用方法的文章就介紹到這了,更多相關Qt線程內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C語言 sprintf 函數(shù)詳情

    C語言 sprintf 函數(shù)詳情

    這篇文章主要介紹了C語言 sprintf 函數(shù),文章主要包括sprintf 函數(shù)簡介、sprintf 函數(shù)使用和簡單說明了一下sprintf、fprintf、printf 函數(shù)區(qū)別,需要的朋友可以參考一下文章的具體內容
    2021-10-10
  • C語言之實現(xiàn)棧的基礎創(chuàng)建

    C語言之實現(xiàn)棧的基礎創(chuàng)建

    這篇文章主要介紹了C語言之實現(xiàn)棧的基礎創(chuàng)建,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-07-07
  • 數(shù)據(jù)結構課程設計- 解析最少換車次數(shù)的問題詳解

    數(shù)據(jù)結構課程設計- 解析最少換車次數(shù)的問題詳解

    數(shù)據(jù)結構課程設計- 解析最少換車次數(shù)的問題詳解
    2013-05-05
  • C++存儲方案和動態(tài)分配

    C++存儲方案和動態(tài)分配

    這篇文章主要介紹了C++存儲方案和動態(tài)分配,
    2021-12-12
  • C++ STL 四種智能指針的用法詳解

    C++ STL 四種智能指針的用法詳解

    C++ 標準模板庫 STL(Standard Template Library) 一共給我們提供了四種智能指針:auto_ptr、unique_ptr、shared_ptr 和 weak_ptr,今天給大家詳細介紹這幾種指針的具體用法,需要的朋友參考下吧
    2021-06-06
  • C++實現(xiàn)學生考勤信息管理系統(tǒng)

    C++實現(xiàn)學生考勤信息管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C++實現(xiàn)學生考勤信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C語言實現(xiàn)快速排序算法實例

    C語言實現(xiàn)快速排序算法實例

    快速排序時間復雜度為O(nlogn),是數(shù)組相關的題目當中經常會用到的算法,下面這篇文章主要給大家介紹了關于C語言實現(xiàn)快速排序算法的相關資料,需要的朋友可以參考下
    2022-06-06
  • C語言數(shù)據(jù)結構進階之棧和隊列的實現(xiàn)

    C語言數(shù)據(jù)結構進階之棧和隊列的實現(xiàn)

    棧和隊列,嚴格意義上來說,也屬于線性表,因為它們也都用于存儲邏輯關系為 "一對一" 的數(shù)據(jù),但由于它們比較特殊,因此將其單獨作為一章,做重點講解
    2021-11-11
  • 詳解C++ 模板編程

    詳解C++ 模板編程

    模板(template)是C++實現(xiàn)泛型(Generics)和元編程(Meta Programming)的基礎。本文拋磚引玉,簡要介紹C++模板編程,不足之處敬請指正。
    2020-09-09
  • C++將txt文件內容保存到數(shù)組的方法

    C++將txt文件內容保存到數(shù)組的方法

    今天小編就為大家分享一篇C++將txt文件內容保存到數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評論