Why QProgressDialog is shown without an explicit call to `exec()` or `show()`?

Viewed 359

I have the following code

#include "dialog.h"

#include <QApplication>
#include <QProgressDialog>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QProgressDialog w;
    //w.show();
    return a.exec();
}

The execution of this code shows a QProgressDialog UI.

enter image description here

I would like to have the explanation why my QProgressDialog shows up without having an exec()or show() instructions. I have read the documentation but did not find the explanation on it.

2 Answers

Both constructors of QProgressDialog call QProgressDialogPrivate::init, where forceTimer : QTimer is started:

 ...
 forceTimer = new QTimer(q);
 QObject::connect(forceTimer, SIGNAL(timeout()), q, SLOT(forceShow()));
 ...
 forceTimer->start(showTime);

The forceShow() slot in turn looks like this:

void QProgressDialog::forceShow() {
    Q_D(QProgressDialog);
    d->forceTimer->stop();
    if (d->shown_once || d->cancellation_flag)
        return;
    show();
    d->shown_once = true;
}

The show(); statement there is the very reason the QProgressDialog is shown on object creation.

Since it will automatically open after being constructed, follow it with a cancellation.

auto * progressDialog = new QProgressDialog{};
progressDialog.cancel();
Related