Qt中QProgressDialog new完后自动弹出问题

问题描述: 在主窗口中初始化进度对话框 通过指针传递将进度对话框传递给各个算法类中已便在需要时候开启进度对话框执行任务。 本人将Qt版本升级为5.7.1后 进度对话框会在初始化时候 就会自动弹出一次。原因是QProgressDialog在初始化函数init()中就将计时器forcetimer开启 所以才会导致我们在初始化程序时候 进度对话框就会跳出来

void QProgressDialogPrivate::init(const QString &labelText, const QString &cancelText,
                                  int min, int max)
{
    Q_Q(QProgressDialog);
    label = new QLabel(labelText, q);
    bar = new QProgressBar(q);
    bar->setRange(min, max);
    int align = q->style()->styleHint(QStyle::SH_ProgressDialog_TextLabelAlignment, 0, q);
    label->setAlignment(Qt::Alignment(align));
    autoClose = true;
    autoReset = true;
    forceHide = false;
    QObject::connect(q, SIGNAL(canceled()), q, SLOT(cancel()));
    forceTimer = new QTimer(q);
    QObject::connect(forceTimer, SIGNAL(timeout()), q, SLOT(forceShow()));//定时器信号槽连接
    if (useDefaultCancelText) {
        retranslateStrings();
    } else {
        q->setCancelButtonText(cancelText);
    }
    starttime.start();
    forceTimer->start(showTime);//开启定时器
}

解决办法:
浏览源码 发现在reset()函数中有计时器的stop函数被调用

void QProgressDialog::reset()
{
    Q_D(QProgressDialog);
#ifndef QT_NO_CURSOR
    if (value() >= 0) {
        if (parentWidget())
            parentWidget()->setCursor(d->parentCursor);
    }
#endif
    if (d->autoClose || d->forceHide)
        hide();
    d->bar->reset();
    d->cancellation_flag = false;
    d->shown_once = false;
    d->setValue_called = false;
    d->forceTimer->stop();//停止定时器
}

所以我们在new完进度对话框后 在调用一下reset函数即可
QProgressDialog *m_progressDlg = new QProgressDialog(this);
m_progressDlg->reset();

猜你喜欢

转载自blog.csdn.net/qing666888/article/details/84895047