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

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

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

举个栗子:

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);
}

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

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);
    }
}

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

5、main.cpp

#include "mainwindow.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

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>

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

ProgressBar

你可能感兴趣的:(QT,qt,c++)