Qt创建任务栏进度条
生活随笔
收集整理的這篇文章主要介紹了
Qt创建任务栏进度条
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、正文
? ? 任務(wù)欄進(jìn)度條是Windows7就引入的一種UI形式,通常用于顯示軟件當(dāng)前正在執(zhí)行的任務(wù)的進(jìn)度(如編譯程序的進(jìn)度、下載任務(wù)的進(jìn)度)。如下:
? ? ?在Qt中使用任務(wù)欄進(jìn)度條也是非常容易的一件事情。Qt框架針對(duì)Windows提供了一個(gè)單獨(dú)的模塊WinExtras。這個(gè)模塊中提供了一些類庫和函數(shù),用于實(shí)現(xiàn)Windows上特有的功能,如類型轉(zhuǎn)換、句柄操作、窗口屬性設(shè)置等。當(dāng)然也包括了此次我們要說的任務(wù)欄進(jìn)度條。那么就直接來看代碼吧:
#include "TaskbarProgress.h"#include <QAbstractButton> #include <QTimer>TaskbarProgress::TaskbarProgress(QWidget *parent): QMainWindow(parent) {ui.setupUi(this);timer = new QTimer;timer->setInterval(1000);timer->setSingleShot(false);windowsTaskbarButton = new QWinTaskbarButton(this); //Create the taskbar button which will show the progressconnect(timer, &QTimer::timeout, this, &TaskbarProgress::onTimeout);connect(ui.startButton, &QAbstractButton::clicked, this, &TaskbarProgress::onButtonClicked); }void TaskbarProgress::onButtonClicked() {windowsTaskbarButton->setWindow(windowHandle()); //Associate the taskbar button to the progress bar, assuming that the progress bar is its own windowwindowsTaskbarProgress = windowsTaskbarButton->progress();windowsTaskbarProgress->setRange(0, 100);timer->start(); }void TaskbarProgress::onTimeout() {windowsTaskbarProgress->setValue(windowsTaskbarProgress->value() + 1);windowsTaskbarProgress->show(); }? ? 代碼邏輯簡(jiǎn)單,點(diǎn)擊開始按鈕之后開始設(shè)置任務(wù)欄進(jìn)度條。注意到這里有個(gè)坑,windowHandle()調(diào)用要在窗口顯示出來之后才能返回正確的窗口句柄,否則返回的null,導(dǎo)致任務(wù)欄進(jìn)度條無法正常顯示。
二、參考鏈接
1.?https://forum.qt.io/topic/70672/windowhandle-will-return-null
總結(jié)
以上是生活随笔為你收集整理的Qt创建任务栏进度条的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【12306图片验证12小时内被破解,验
- 下一篇: linux下如何查看chm文件