【Qt】任务栏显示进度条

Qt 任务栏显示进度条

注意事项:

  1. 修改pro文件
  2. 界面展示后再 new QWinTaskbarButton,否则会不显示

# Qt 官方demo

1
2
3
4
5
6
7
QWinTaskbarButton *button = new QWinTaskbarButton(widget);
button->setWindow(widget->windowHandle());
button->setOverlayIcon(QIcon(":/loading.png"));

QWinTaskbarProgress *progress = button->progress();
progress->setVisible(true);
progress->setValue(50);

# Demo

1 修改pro文件

1
win32:QT += winextras

2 .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
28
29
30
31
32
33
34
35
36
37
#ifndef TASKBARPROGRESS_H
#define TASKBARPROGRESS_H

#include <QMainWindow>
#include <QTimer>
#include <QAbstractButton>
#include <QWinTaskbarProgress>
#include <QWinTaskbarButton>


namespace Ui {
class TaskbarProgress;
}

class TaskbarProgress : public QMainWindow
{
Q_OBJECT

public:
explicit TaskbarProgress(QWidget *parent = 0);
~TaskbarProgress();

void onButtonClicked();

void onTimeout();

private:
Ui::TaskbarProgress *ui;

QTimer *timer;

QWinTaskbarButton *windowsTaskbarButton;

QWinTaskbarProgress *windowsTaskbarProgress;
};

#endif // TASKBARPROGRESS_H

3 .cpp

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
#include "taskbarprogress.h"
#include "ui_taskbarprogress.h"

TaskbarProgress::TaskbarProgress(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TaskbarProgress)
{
ui->setupUi(this);

timer = new QTimer;
timer->setInterval(1000); //设置时间间隔

//创建显示进度的任务栏按钮
windowsTaskbarButton = new QWinTaskbarButton(this);
connect(timer, &QTimer::timeout, this, &TaskbarProgress::onTimeout);
connect(ui->Btn_Start, &QAbstractButton::clicked, this, &TaskbarProgress::onButtonClicked);
}

TaskbarProgress::~TaskbarProgress()
{
delete ui;
}

void TaskbarProgress::onButtonClicked() {
//将任务栏按钮关联到进度栏,假设进度栏是它自己的窗口
windowsTaskbarButton->setWindow(windowHandle());
windowsTaskbarProgress = windowsTaskbarButton->progress();
windowsTaskbarProgress->setRange(0, 100);
timer->start();
}

void TaskbarProgress::onTimeout() {
//当进度条的值发生变化时,更改任务栏中的进度值
windowsTaskbarProgress->setValue(windowsTaskbarProgress->value() + 20);
windowsTaskbarProgress->show();
if (windowsTaskbarProgress->value() > 99) {
windowsTaskbarProgress->setValue(0);
timer->stop();
}
}

【Qt】任务栏显示进度条
https://hodlyounger.github.io/B_Code/Qt/动画/【Qt】任务栏进度条/
作者
mingming
发布于
2023年10月27日
许可协议