您好,登錄后才能下訂單哦!
自定義信號(hào)與槽
Qt多線程簡單實(shí)現(xiàn)
多線程間變量傳遞
主線程類:
//export_key.h#ifndef EXPORT_KEY_H#define EXPORT_KEY_H#include "main_widget.h"namespace Ui { class export_key; } class texport_work; class export_key : public QWidget { Q_OBJECTpublic: explicit export_key(QWidget *parent = 0); ~export_key();public slots: int send_cmd(int *len, uchar *pk);private: Ui::export_key *ui; //工作線程對(duì)象指針,在類實(shí)現(xiàn)中new對(duì)象work_thread texport_work *work_thread; uchar cmd[1024]; uchar kek[16]; };#endif // EXPORT_KEY_H12345678910111213141516171819202122232425262728291234567891011121314151617181920212223242526272829
//export_key.cpp#include "export_key.h"#include "ui_export_key.h"#include <QMessageBox>//#define printpkexport_key::export_key(QWidget *parent) : QWidget(parent), ui(new Ui::export_key) { ui->setupUi(this); //實(shí)例化 工作線程的對(duì)象 **work_thread = new texport_work();** //連接主線程和工作線程,信號(hào)處于工作線程,槽位于當(dāng)前主線程,注意保持信號(hào)和槽的參數(shù)一致! //connect的第五個(gè)參數(shù)必須為阻塞隊(duì)列連接,這樣才能保證信號(hào)傳遞過來的參數(shù)和槽接收的是實(shí)時(shí)的! **connect(work_thread,SIGNAL(send_export_signal(int *,uchar *)),this,SLOT(send_cmd(int *,uchar*)),Qt::BlockingQueuedConnection);** }//槽的實(shí)現(xiàn)int export_key::send_cmd(int *len,uchar* pk) { work_thread->stop(); //接收來自工作線程信號(hào)傳遞過來的參數(shù) mymemcpy(cmd,pk,*len);#ifdef printpk int n = 0; uchar cmd2[2048] = {0}; n = hextostr(cmd,*len,cmd2); cmd2[n] = '\0'; qDebug("send pk:%s \n",cmd2);#endif ui->pbenter->setEnabled(true); return ErrCode_NoErr; } export_key::~export_key() { delete ui; }1234567891011121314151617181920212223242526272829303132333435363738394041424344454612345678910111213141516171819202122232425262728293031323334353637383940414243444546
工作線程類:
#ifndef EXPORT_WORK_H#define EXPORT_WORK_H#include <QThread>#include "main_widget.h"http://繼承Qthread基類,重寫虛函數(shù)runclass texport_work : public QThread { Q_OBJECTpublic: texport_work();protected: void run();//自定義信號(hào)**signals: void send_export_signal(int *,uchar *);**private: volatile int start_export; };#endif // EXPORT_WORK_H12345678910111213141516171819202122232425261234567891011121314151617181920212223242526
//export_work.cpp#include "export_work.h"#include "export_key.h"#include <QThread>texport_work::texport_work() { start_export = 0; }//重寫虛函數(shù)run,在虛函數(shù)中實(shí)現(xiàn)要在工作線程中要做的事情,開啟事件循環(huán)void texport_work::run() { while(!start_export) { qDebug()<<"start thread!"; timerThread();//自定義函數(shù) } start_export = 0; }void texport_work::stop() { start_export = 1; }void texport_work::timerThread() { int len = 0; uchar buf[1024]; len = serial::GetSerialPtr()->recvcommdata(buf); if(len <= 0) return; if(buf[0] == 0xAA) { if(buf[1] || buf[2] || buf[3] || buf[4] != 16) return; //發(fā)射信號(hào),自定義信號(hào)send_export_signal并將變量len和數(shù)組buf傳遞給槽函數(shù)。 emit send_export_signal(&len,buf); } }12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849501234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
總結(jié):
1.跨線程連接信號(hào)與槽時(shí)注意連接方式,也就是connect函數(shù)第五個(gè)參數(shù)
2.實(shí)現(xiàn)多線程方式,工作線程繼承Qthread基類,重寫虛函數(shù)run在run中實(shí)現(xiàn)工作線程需要做的事情;在主線程中創(chuàng)建工作線程的實(shí)例,可以對(duì)工作線程進(jìn)行控制。
3.多線程間傳遞變量的一種是直接通過信號(hào)與槽的連接實(shí)現(xiàn),注意連接方式
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。