zoukankan      html  css  js  c++  java
  • Qt创建任务栏进度条

    一、正文

        任务栏进度条是Windows7就引入的一种UI形式,通常用于显示软件当前正在执行的任务的进度(如编译程序的进度、下载任务的进度)。如下:

         在Qt中使用任务栏进度条也是非常容易的一件事情。Qt框架针对Windows提供了一个单独的模块WinExtras。这个模块中提供了一些类库和函数,用于实现Windows上特有的功能,如类型转换、句柄操作、窗口属性设置等。当然也包括了此次我们要说的任务栏进度条。那么就直接来看代码吧:

    #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 progress
        connect(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 window
        windowsTaskbarProgress = windowsTaskbarButton->progress();
        windowsTaskbarProgress->setRange(0, 100);
        timer->start();
    }
    
    void TaskbarProgress::onTimeout() {
        windowsTaskbarProgress->setValue(windowsTaskbarProgress->value() + 1);
        windowsTaskbarProgress->show();
    }

        代码逻辑简单,点击开始按钮之后开始设置任务栏进度条。注意到这里有个坑,windowHandle()调用要在窗口显示出来之后才能返回正确的窗口句柄,否则返回的null,导致任务栏进度条无法正常显示。

    二、参考链接

    1. https://forum.qt.io/topic/70672/windowhandle-will-return-null

  • 相关阅读:
    查看SQL Server版本号(2005 & 2008)
    Installing an App for Testing
    Viewstate 的用法
    How to Open the Pdf file?
    工具类:Log
    SPSiteDataQuery and SPQuery
    SPSite, SPWeb Dispose and Class Design Partter
    Add Properties or Delete List Folder Content Type
    SharePoint UserControl
    Click Once 布署
  • 原文地址:https://www.cnblogs.com/csuftzzk/p/qt_task_bar_progress.html
Copyright © 2011-2022 走看看