• QT5|C++|通过信号槽机制实现进度条更新


    背景:最近在写一个删除90天数据显示进度的功能,实现思路是:通过信号槽捕获当前进度值实现。

     备注:点击start按钮,开始更新进度条,直到100(每隔1s进行更新)
    
    • 1

    举个栗子:

    1、mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::close);
        connect(ui->pushButton1,&QPushButton::clicked,this,&MainWindow::on_startProcess);
    
        void (MyThread::*rmsgSignal)(int) = &MyThread::msgSignal;
        connect(&thread,rmsgSignal,this,&MainWindow::on_setProcess);
    
        ui->progressBar->setRange(0,100);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    //开启线程
    void MainWindow::on_startProcess(){
        thread.start();
    
    }
    
    //设置进度条参数
    void MainWindow::on_setProcess(int v){
        ui->progressBar->setValue(v);
    }
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    2、mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include 
    
    #include 
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        MyThread thread;
    
        void on_startProcess();
        void on_setProcess(int v);
    
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    • 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
    • 27

    3、mythread.cpp

    #include "mythread.h"
    
    MyThread::MyThread()
    {
    
    }
    
    //重写run方法
    void MyThread::run(){
        for(int i = 1;i<=100;i++){
            QThread::msleep(1000);
            emit msgSignal(i);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4、MyThread.h

    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    #include 
    
    
    class MyThread: public QThread{
        Q_OBJECT
    public:
        MyThread();
        void run() override;
    
    signals:
        void msgSignal(int a);
    };
    
    #endif // MYTHREAD_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5、main.cpp

    #include "mainwindow.h"
    
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6、mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QWidget" name="verticalLayoutWidget">
        <property name="geometry">
         <rect>
          <x>320</x>
          <y>140</y>
          <width>160</width>
          <height>80</height>
         </rect>
        </property>
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QPushButton" name="pushButton">
           <property name="text">
            <string>close</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="pushButton1">
           <property name="text">
            <string>start</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QProgressBar" name="progressBar">
           <property name="value">
            <number>0</number>
           </property>
           <property name="format">
            <string>%v%</string>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>23</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    7、结果
    注:视频为测试结果,仅录制部分作为参考

    ProgressBar

  • 相关阅读:
    自动化测试 —— 基于Jmeter之DDT!
    复盘:细数这些年写文字的成与败
    《opencv学习笔记》-- SURF 特征提取
    Milvus踩坑笔记
    八皇后问题
    Linux的查找,压缩,别名,用户管理的相关命令
    EF7学习指南
    Linux----Ubuntu系统官网下载iso镜像文件
    算法-26. 删除有序数组中的重复项-⭐
    新概念英语(第二册)复习——Lesson 6 - Lesson10
  • 原文地址:https://blog.csdn.net/u011735367/article/details/132980073