Qt中线程的使用1
                            
                            
                            初學Qt線程,此實例只是對Qt單一線程的使用,另外結合信號槽實現ui界面與子線程之間數據的傳輸,可以解決線程在運行的過程中,關閉對話框程序崩潰的問題。
 
                        
                        
                        此實例要實現的效果圖如下:
 
 界面控件的類,對象表如下:
 
 界面啟動后,起初停止線程按鈕是置灰的,點擊開始線程按鈕,停止線程按鈕不再置灰,在textBrowser中顯示1 2 3 4…等數字,點擊停止線程按鈕會停止打印,注:點擊停止線程后不會立即停止打印,而是有一定的延時,再次點擊開始線程按鈕,textBrowser中繼續顯示數字,只要顯示的數字小于某一個數值,不點擊停止線程按鈕或者關閉對話框,線程就會一直運行,另外可以在點擊開始線程按鈕前后,可以點擊GetTime按鈕和Smile按鈕。
 可以分為線程類的編寫,對話框類對線程的使用,代碼如下:
 MyThread.h
MyThread.h.cpp
#include "mythread.h" #include <QDateTime>MyThread::MyThread(QObject * parent):QThread (parent),m_a(0),m_Status(false) {}MyThread::~MyThread() {}void MyThread::run() {m_Status = false;while(m_a < 30000){sleep(1);m_a++;emit signString(tr("%1 ").arg(m_a));QMutexLocker mutex(&m_mutex);if(m_Status){return ;}} }void MyThread::DoSomething() {QDateTime current_date_time =QDateTime::currentDateTime();QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");emit signGetTime(current_date); }void MyThread::DislaySomthing() {emit signSmileString(tr("if you belive yourself,you can do it at most persent of 99.")); }void MyThread::StopThread() {QMutexLocker mutex(&m_mutex);m_Status = true; }Dialog.h
#ifndef DIALOG_H #define DIALOG_H#include <QDialog>class MyThread;namespace Ui { class Dialog; }class Dialog : public QDialog {Q_OBJECTpublic:explicit Dialog(QWidget *parent = nullptr);~Dialog();private slots:void on_pushButtonGetTime_clicked();void on_pushButtonSmile_clicked();void on_pushButtonStart_clicked();void on_pushButtonStop_clicked();void slotGetTime(QString & str);void slotDisplaySmile(const QString &str);void slotString(const QString &Str); private:Ui::Dialog *ui;MyThread *m_Thread; };#endif // DIALOG_HDialog.cpp
#include "dialog.h" #include "ui_dialog.h" #include "mythread.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog) {ui->setupUi(this);m_Thread = new MyThread(this);connect(m_Thread,&MyThread::signGetTime,this,&Dialog::slotGetTime);connect(m_Thread,&MyThread::signSmileString,this,&Dialog::slotDisplaySmile);connect(m_Thread,&MyThread::signString,this,&Dialog::slotString); }Dialog::~Dialog() {if(m_Thread->isRunning()){m_Thread->StopThread();m_Thread->wait();}delete ui; }void Dialog::on_pushButtonGetTime_clicked() {m_Thread->DoSomething(); }void Dialog::on_pushButtonSmile_clicked() {m_Thread->DislaySomthing(); }void Dialog::on_pushButtonStart_clicked() {if(m_Thread->isRunning()){return ;}m_Thread->start();ui->pushButtonStop->setEnabled(true); }void Dialog::on_pushButtonStop_clicked() {m_Thread->StopThread();ui->pushButtonStop->setEnabled(false); }void Dialog::slotGetTime(QString &str) {ui->lineEditTime->setText(str); }void Dialog::slotDisplaySmile(const QString &str) {ui->textEditSmile->setText(str); }void Dialog::slotString(const QString &Str) {QString strSrc = ui->textBrowser->toPlainText();strSrc += Str;ui->textBrowser->setText(strSrc); }main.cpp
#include "dialog.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Dialog w;w.show();return a.exec(); }以上是一個簡單的線程,總結下來,繼承QThread的線程類,必須在該類中重寫run()函數,在run()函數中編寫線程要做的事,線程啟動時通過線程類對象或指針調用start()函數,線程關閉時可以使用bool變量來控制線程的關閉,也可通過quit(),wait()來等待線程的關閉,不建議使用terminate()來終止正在運行的線程,因為這樣線程的終止取決于系統的調用策略,即隨機調用性。
總結
                            
                        - 上一篇: linux按日期备份mysql,在Lin
 - 下一篇: 使用说明_预拌混凝土使用说明