Qt學習之如何啟動和終止乙個執行緒

2021-08-20 05:43:13 字數 1942 閱讀 1826

1、qt執行緒池實驗研究與分析(qthread與和qthreadpool + qrunnable使用上的區別)----

2、qrunnable中,如何接收tcp連線資訊----

今天來看看qt如何啟動乙個執行緒吧,**就以精通qt4程式設計的為例

先來給出每個檔案的相關**然後再加以分析

cpp**  

//*************dialog.h**************//

#ifndef dialog_h

#define dialog_h

#include 

#define maxsize 5  //最大的執行緒數

class qdialogbuttonbox;  

class qprogressbar;  

class qpushbutton;  

class workthread;  

class threaddlg : public qdialog  

;  #endif

//***********end end end************//

cpp**  

//***********mainwindow.h************//

#ifndef workthread_h

#define workthread_h

#include 

class workthread : public qthread  

;  #endif

//***********end end end************//

cpp**  

//***********dialog.cpp************//

#include 

#include "workthread.h"

#include "dialog.h"

threaddlg::threaddlg(qwidget *parent)       

: qdialog(parent)  

void threaddlg::start()  

for(int i=0;i  

stopbutton->setenabled(true);  

startbutton->setenabled(false);  

}  void threaddlg::stop()  

startbutton->setenabled(true);  

stopbutton->setenabled(false);  

}  //***********end end end************//

cpp**  

//***********mainwindow.cpp************//

#include "workthread.h"

#include "dialog.h"

#include 

#include 

#include 

void workthread::run()  

}  //***********end end end************//

//***********main.cpp************//

#include 

#include "dialog.h"

int main(int argc, char *argv)  

//***********end end end************//

以上**簡單,沒有必要做過多的講解,但是其中的「threadvector[i]->terminate(); 」有必要講解下,terminate()函式的呼叫便不會立刻終止執行緒,因為執行緒的何時終止取決於系統的排程策略,所在在之後又呼叫了wait()函式是執行緒阻塞等待直到退出或者超時。

最後加以一點就是在.pro檔案中加入一行**才能成功執行:

config+=thread

QT學習 如何分析乙個QT類

出處 我們以qlineedit這個類為例來看看如何學習分析乙個qt類。1.public types 這是乙個在這個類中可以供我們使用的公共列舉型別。這也就是說,我們可以如下使用這個公共型別 some code qlineedit echomode mode mode mylineedit echom...

QT學習 如何分析乙個QT類

出處 我們以qlineedit這個類為例來看看如何學習分析乙個qt類。1.public types 這是乙個在這個類中可以供我們使用的公共列舉型別。這也就是說,我們可以如下使用這個公共型別 some code qlineedit echomode mode mode mylineedit echom...

如何啟動乙個專案

在花了幾年時間管理各種技術組織的專案經理後,我總是驚訝地發現,許多專案經理都在開始專案的時候犯難。分配給他們乙個新專案,在幾個星期,有時候甚至是幾個月之後,他們都不知道該怎麼做才能推進他們的專案,而且在這段時間裡做的很少或什麼也沒有做。難道是專案經理不完全理解任務,不熟悉他們關於這個主題的業務知識,...