QT 線程暫停,繼續執行的一種實現(有些道理,而且封裝了)


注意:本次實現線程的暫停執行主要采用互斥量的方法,如果有更好的實現方法的小伙伴可以在下面留言!

直接插入代碼了,由於做的小demo,代碼寫的可能有點亂,但還算完整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QDebug>
#include <QThread>
#include <QMutex>
class  MyThread: public  QThread
{
     Q_OBJECT
public :
     MyThread();
     ~MyThread();
     void  run();
public  slots:
     void  threadStart();
     void  threadPause();
     void  threadStop();
     void  threadResume();
     void  threadPR();
private :
     bool  m_buttonState;  //if pause m_buttonState=false;else m_buttonState=true;
     int  m_i;
     QMutex m_mutex; //互斥量
};
 
#endif // MYTHREAD_H

 

復制代碼
 1 //mythread.cpp
 2 #include "mythread.h"
 3 MyThread::MyThread()
 4 {
 5     m_i=0;
 6     m_buttonState=false;
 7 }
 8 
 9 MyThread::~MyThread()
10 {
11 
12 }
13 
14 void MyThread::run()
15 {
16     m_buttonState=true;
17     while(1)
18     {
19         m_mutex.lock();
20         m_i++;
21         qDebug()<<QString("the value of m_i is %1 ").arg(m_i);
22         m_mutex.unlock();
23         this->sleep(1);
24     }
25 
26 
27 
28 }
29 
30 
31 void MyThread::threadPause()
32 {
33     qDebug()<<QString("pause :%1").arg(m_buttonState);
34         this->m_mutex.lock();
35         this->m_buttonState=false;
36         qDebug()<<QString("pause");
37 }
38 void MyThread::threadResume()
39 {
40       qDebug()<<QString("resume :%1").arg(m_buttonState);
41         this->m_mutex.unlock();
42         this->m_buttonState=true;
43         qDebug()<<QString("resume");
44 
45 }
46 void MyThread::threadStop()
47 {
48     this->exit();
49 
50 }
51 void MyThread::threadStart()
52 {
53     this->start();
54 }
55 void MyThread::threadPR()
56 {
57     if(m_buttonState)
58     {
59         threadPause();
60 
61     }
62     else
63     {
64         threadResume();
65     }
66 
67 }
復制代碼
復制代碼
 1 //mainwindow.h
 2 #ifndef MAINWINDOW_H
 3 #define MAINWINDOW_H
 4 
 5 #include <QMainWindow>
 6 #include "mythread.h"
 7 namespace Ui {
 8 class MainWindow;
 9 }
10 
11 class MainWindow : public QMainWindow
12 {
13     Q_OBJECT
14 
15 public:
16     explicit MainWindow(QWidget *parent = 0);
17     ~MainWindow();
18 private slots:
19     void changeButton();
20     void threadStop();
21     void threadStart();
22     void threadPR();
23 private:
24     Ui::MainWindow *ui;
25     MyThread *myThread;
26     MyThread *oneThread;
27 
28 };
29 
30 #endif // MAINWINDOW_H
復制代碼
復制代碼
 1 //mainwindow.cpp
 2 #include "mainwindow.h"
 3 #include "ui_mainwindow.h"
 4 
 5 MainWindow::MainWindow(QWidget *parent) :
 6     QMainWindow(parent),
 7     ui(new Ui::MainWindow)
 8 {
 9     ui->setupUi(this);
10     myThread=new MyThread;
11     oneThread=new MyThread;
12     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(changeButton()));
13     connect(ui->startButton,SIGNAL(clicked()),this,SLOT(threadStart()));
14     connect(ui->pauseButton,SIGNAL(clicked()),this,SLOT(threadPR()));
15     connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(threadStop()));
16 }
17 
18 MainWindow::~MainWindow()
19 {
20     if(ui!=NULL)
21     {
22         delete ui;
23         ui=NULL;
24     }
25     if(myThread!=NULL)
26     {
27         delete myThread;
28         myThread=NULL;
29     }
30     if(oneThread!=NULL)
31     {
32         delete oneThread;
33         oneThread=NULL;
34     }
35 }
36 
37 void MainWindow::changeButton()
38 {
39 
40 
41     if(!QString::compare(ui->pauseButton->text(),QString::fromUtf8("暫停")))
42     {
43         ui->pauseButton->setText(QString::fromUtf8("繼續"));
44     }else
45     {
46         ui->pauseButton->setText(QString::fromUtf8("暫停"));
47     }
48 }
49 
50 void MainWindow::threadStart()
51 {
52     myThread->threadStart();
53     oneThread->threadStart();
54 
55 }
56 void MainWindow::threadStop()
57 {
58     myThread->terminate();
59     oneThread->terminate();
60 
61 }
62 void MainWindow::threadPR()
63 {
64     myThread->threadPR();
65     oneThread->threadPR();
66 
67 
68 }
復制代碼

還有一個簡單的ui界面就不貼了,就三個button

暫停、繼續就可以實現了,但很奇怪的事情是當將線程terminate 后再調用start 也會好像出現“暫停、繼續”的效果,這個正在思考中,如果小伙伴有知道的留言tell me啊!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM