Chris
Chris

Reputation: 991

What am I doing wrong with QWinTaskbarProgress?

I followed the examples I found to make use of the QWinTaskbarProgress. I created a standard Qt Widgets Application in Qt Creator (Qt 5.3.1) and my mainwindow.cpp looks like this:

#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;
}

I expected the task bar icon to be overlaid and showing 50% progress bar after starting the application, but the task bar looks normal, just as if did not code anything. What am I doing wrong?

Upvotes: 4

Views: 1138

Answers (2)

Kervala
Kervala

Reputation: 496

In fact, it seems like calling "m_taskbarButton->setWindow(windowHandle());" in QMainWindow constructor doesn't work and QWinTaskbarProgress won't show at all even after calling setVisible(true) or show().

It has to be called once the Window is shown like in :

void MainWindow::showEvent(QShowEvent *e)
{
#ifdef Q_OS_WIN32
    m_taskbarButton->setWindow(windowHandle());
#endif

    e->accept();
}

Upvotes: 7

Jablonski
Jablonski

Reputation: 18504

Your and my code very similar to example in Qt Documentation. I have no idea why, but this doesn't work on my computer too. But I found solution:

Create singleShot and set progress in slot:

In header:

private slots:    
    void echo();

In constructor:

QTimer::singleShot(1000,this,SLOT(echo()));
QTimer::singleShot(10,this,SLOT(echo()));//works too

Slot:

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);
}

And now it works!

Upvotes: 0

Related Questions