[英]What am I doing wrong with QWinTaskbarProgress?
我按照我發現的例子來使用QWinTaskbarProgress
。 我在Qt Creator
(Qt 5.3.1)中創建了一個標准的Qt Widgets Application
,我的mainwindow.cpp
看起來像這樣:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_taskbarButton = new QWinTaskbarButton(this);
m_taskbarButton->setWindow(windowHandle());
m_taskbarButton->setOverlayIcon(style()->standardIcon(QStyle::SP_MediaPlay));
m_taskbarProgress = m_taskbarButton->progress();
m_taskbarProgress->setVisible(true);
m_taskbarProgress->setRange(0, 100);
m_taskbarProgress->setValue(50);
}
MainWindow::~MainWindow()
{
delete ui;
}
我希望在啟動應用程序后,任務欄圖標會被覆蓋並顯示50%
進度條,但任務欄看起來很正常,就好像沒有編寫任何代碼一樣。 我究竟做錯了什么?
實際上,它似乎調用“m_taskbarButton-> setWindow(windowHandle());” 在QMainWindow中,構造函數不起作用,即使在調用setVisible(true)或show()之后,QWinTaskbarProgress也根本不會顯示。
一旦Window顯示如下,就必須調用它:
void MainWindow::showEvent(QShowEvent *e)
{
#ifdef Q_OS_WIN32
m_taskbarButton->setWindow(windowHandle());
#endif
e->accept();
}
您和我的代碼與Qt Documentation
示例非常相似。 我不明白為什么,但這在我的電腦上也不起作用。 但我找到了解決方案:
創建singleShot
並在插槽中設置進度:
在標題中:
private slots:
void echo();
在構造函數中:
QTimer::singleShot(1000,this,SLOT(echo()));
QTimer::singleShot(10,this,SLOT(echo()));//works too
插槽:
void MainWindow::echo()
{
QWinTaskbarButton *button = new QWinTaskbarButton(this);
button->setWindow(windowHandle());
button->setOverlayIcon(style()->standardIcon(QStyle::SP_MediaPlay));
QWinTaskbarProgress *progress = button->progress();
progress->setVisible(true);
progress->setRange(0, 100);
progress->setValue(50);
}
現在它有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.