4

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?

Chris
  • 981
  • 4
  • 14
  • 29
  • possible duplicate of [QWinTaskbarProgress won't show](http://stackoverflow.com/questions/24840941/qwintaskbarprogress-wont-show) – George Hilliard Jun 23 '15 at 21:28

2 Answers2

8

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();
}
Kervala
  • 516
  • 1
  • 7
  • 7
  • Thanks for your answer. So, since you put it in this define, does it behave differently on non-Windows systems then? Could we call it a bug, then? – Chris Nov 14 '14 at 16:36
  • You're welcome :) Yes, I suspect it's a bug because a QWindow should be identical in shown or hidden state :( Btw the #ifdef is because QWinTaskbarButton only exists under Windows (it's a QtWinExtras library). – Kervala Nov 14 '14 at 17:27
  • Still a workaround, but I like it more than others. Sorry about my question on the define, I must have been quite tired when I asked that ;-) – Chris Dec 01 '14 at 12:11
  • `Q_OS_WIN` is a better macro, since it works on both 32 and 64-bit systems. Also, do you have to call setWindow() on every single showEvent? Calling it once is not enough? – KovBal Apr 27 '15 at 21:20
  • Yes, I agree the name is perhaps better. But Q_OS_WIN32 and Q_OS_WIN64 are both defined in 64 bits as well as Q_OS_WIN. – Kervala Apr 28 '15 at 08:41
  • Yes, this solution works. If even this does not work for you though, you might need to call `m_taskbarButton->progress()->show()` AFTER the window handle is set. – HiFile.app - best file manager Apr 11 '18 at 19:57
0

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!

Jablonski
  • 18,083
  • 2
  • 46
  • 47