为什么 QProgressDialog 显示时没有明确调用 `exec()` 或 `show()`?

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

我有以下代码

#include "dialog.h"

#include <QApplication>
#include <QProgressDialog>

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

这段代码的执行显示了一个 QProgressDialog UI。

我想解释一下为什么我的 QProgressDialog 在没有 exec()show() 指令的情况下出现。 我已经阅读了文档,但没有找到解释。

QProgressDialog call QProgressDialogPrivate::init 的两个构造函数,其中 forceTimer : QTimer 开始:

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

forceShow() 插槽依次如下所示:

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

show(); 语句正是在创建对象时显示 QProgressDialog 的原因。

由于建造后会自动打开,所以跟着取消。

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