Open
Description
Qt任务栏显示进度条
Qt 任务栏显示进度条
> 注意事项:
>
> 1. 修改pro文件
> 2. 界面展示后再 new QWinTaskbarButton
,否则会不显示
# Qt 官方demo
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文件
win32:QT += winextras
2 .h
#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
#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();
}
}
blog link Qt任务栏显示进度条