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

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

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

举个栗子:

1、mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QThread>

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

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <mythread.h>

#include <QMainWindow>

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

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

cpp 复制代码
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>


class MyThread: public QThread{
    Q_OBJECT
public:
    MyThread();
    void run() override;

signals:
    void msgSignal(int a);
};

#endif // MYTHREAD_H

5、main.cpp

cpp 复制代码
#include "mainwindow.h"

#include <QApplication>

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

6、mainwindow.ui

cpp 复制代码
<?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

相关推荐
weixin_515033931 分钟前
ccfcsp-202006(4、5)
c++·算法
sukalot12 分钟前
windows C++ 并行编程-异步消息块(五)
c++·windows
姝孟18 分钟前
C++——类和对象
开发语言·c++
侯孟禹22 分钟前
C++ 新特性
c++
极客小张26 分钟前
基于正点原子Linux开发板的智能监控与家电控制系统设计:深度解析Video4Linux和TCP/IP技术栈
linux·运维·c++·物联网·网络协议·tcp/ip·算法
周哈里窗的编程2 小时前
CSP-CCF★201912-2回收站选址★
c++·算法·图论
未来可期LJ5 小时前
【C++ 设计模式】单例模式的两种懒汉式和饿汉式
c++·单例模式·设计模式
Trouvaille ~5 小时前
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
c++·c++20·编译原理·编译器·类和对象·rvo·nrvo
little redcap5 小时前
第十九次CCF计算机软件能力认证-乔乔和牛牛逛超市
数据结构·c++·算法
机器视觉知识推荐、就业指导6 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt