一.功能说明

在Qt中实现固件升级的进度条显示窗口,你可以通过创建一个自定义的对话框(Dialog)来完成。这个对话框可以包含一个进度条(QProgressBar)、一些文本标签(QLabel)用于显示状态信息,以及可能的按钮(如取消按钮)来允许用户中断升级过程(尽管在固件升级期间取消操作可能不被支持或需要谨慎处理)。

二.代码实现

1.frmupgrade.h

#ifndef FRMUPGRADE_H

#define FRMUPGRADE_H

#include <QDialog>

#include <QTimer>

namespace Ui {

class frmupgrade;

}

class frmupgrade : public QDialog

{

    Q_OBJECT

public:

    explicit frmupgrade(QWidget *parent = nullptr);

    ~frmupgrade();

 public:

    void upgrade_start();

private slots:

    void Timer1UpDate();

    void on_pushButton_clicked();

protected:

    void closeEvent(QCloseEvent *event) override;

private:

    Ui::frmupgrade *ui;

    void Init();

    QTimer *timer1 = nullptr;

    int count;

};

#endif // FRMUPGRADE_H

2.frmupgrade.cpp

#include "frmupgrade.h"

#include "ui_frmupgrade.h"

frmupgrade::frmupgrade(QWidget *parent) :

    QDialog(parent),

    ui(new Ui::frmupgrade)

{

    ui->setupUi(this);

    Init();

}

frmupgrade::~frmupgrade()

{

    delete ui;

}

void frmupgrade::closeEvent(QCloseEvent *event)

{

    if(timer1 != nullptr){

        timer1->stop();

        delete timer1;

        timer1 = nullptr;

    }

}

/*升级界面初始化*/

void frmupgrade::Init()

{

    ui->label_upgrade->hide();

    ui->pushButton->hide();

    ui->progressBar->setMinimum(0);

    ui->progressBar->setMaximum(100);

    ui->progressBar->setValue(0);

    ui->progressBar->setStyleSheet("QProgressBar{border:1px solid skyblue;"

                                   "background:white;"

                                   "text-align:center;"

                                   "color: red;"

                                   "border-radius:0px;}"

                                   "QProgressBar::chunk{"

                                   "border-radius:0px;"    // 斑马线圆角

                                   "border:0px solid black;" // 黑边,默认无边

                                   "background-color:green;"

                                   "width:30px;margin:0.5px;}" // 宽度和间距

                                   );

    ui->progressBar->setOrientation(Qt::Horizontal);

    count = 0;

    if(timer1 != nullptr)

    {

        delete timer1;

        timer1 = nullptr;

    }

    timer1 = new QTimer();

    timer1->start(200);

    connect(timer1, SIGNAL(timeout()), this, SLOT(Timer1UpDate()));

}

/*定时器读取升级进度*/

void frmupgrade::Timer1UpDate()

{

    count = count+2 ;   //10s进度条完成

    ui->progressBar->setValue(count);

    if(count>=100){

        timer1->stop();

        delete timer1;

        timer1 = nullptr;

        ui->label_upgrade->show();

        ui->pushButton->show();

        ui->label_upgrade->setText(tr("升级完成,请5秒后再重启设备!"));

          

    }

}

/*关闭窗口*/

void frmupgrade::on_pushButton_clicked()

{

    this->close();

}

void frmupgrade::upgrade_start()

{

    Init();

    this->show();

}

3.界面展示

QT实现升级进度条页面-LMLPHP

完整工程代码下载:https://mp.csdn.net/mp_download/manage/download/UpDetailed

09-23 10:59