76 label =
new QLabel(labelText, q);
77 bar =
new QProgressBar(q);
78 bar->setRange(min, max);
79 int align = q->style()->styleHint(QStyle::SH_ProgressDialog_TextLabelAlignment,
nullptr, q);
80 label->setAlignment(Qt::Alignment(align));
81 QObject::connect(q, SIGNAL(canceled()), q, SLOT(cancel()));
82 forceTimer =
new QTimer(q);
83 QObject::connect(forceTimer, SIGNAL(timeout()), q, SLOT(forceShow()));
87 q->setCancelButtonText(cancelText);
90 forceTimer->start(showTime);
96 int sp = q->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing,
nullptr, q);
97 int mb = q->style()->pixelMetric(QStyle::PM_LayoutBottomMargin,
nullptr, q);
98 int ml = qMin(q->width() / 10, q->style()->pixelMetric(QStyle::PM_LayoutLeftMargin,
nullptr, q));
99 int mr = qMin(q->width() / 10, q->style()->pixelMetric(QStyle::PM_LayoutRightMargin,
nullptr, q));
100 const bool centered =
101 bool(q->style()->styleHint(QStyle::SH_ProgressDialog_CenterCancelButton,
nullptr, q));
103 int additionalSpacing = 0;
105 QSize bh =
bar->sizeHint();
111 for (
int attempt=5; attempt--;) {
112 cspc =
cancel ? cs.height() + sp : 0;
113 lh = qMax(0, q->height() - mb - bh.height() - sp - cspc);
115 if (lh < q->height()/4) {
120 cs.setHeight(qMax(4,cs.height()-sp-2));
122 bh.setHeight(qMax(4,bh.height()-sp-1));
130 centered ? q->width()/2 - cs.width()/2 : q->width() - mr - cs.width(),
131 q->height() - mb - cs.height(),
132 cs.width(), cs.height());
136 label->setGeometry(ml, additionalSpacing, q->width() - ml - mr, lh);
137 bar->setGeometry(ml, lh + sp + additionalSpacing, q->width() - ml - mr, bh.height());
148 Q_Q(QProgressDialog);
149 if (receiverToDisconnectOnClose) {
150 QObject::disconnect(q, SIGNAL(canceled()), receiverToDisconnectOnClose,
151 memberToDisconnectOnClose);
152 receiverToDisconnectOnClose =
nullptr;
154 memberToDisconnectOnClose.clear();
247QProgressDialog::QProgressDialog(QWidget *parent, Qt::WindowFlags f)
248 : QDialog(*(
new QProgressDialogPrivate), parent, f)
250 Q_D(QProgressDialog);
251 d->useDefaultCancelText =
true;
252 d->init(QString::fromLatin1(
""), QString(), 0, 100);
277QProgressDialog::QProgressDialog(
const QString &labelText,
278 const QString &cancelButtonText,
279 int minimum,
int maximum,
280 QWidget *parent, Qt::WindowFlags f)
281 : QDialog(*(
new QProgressDialogPrivate), parent, f)
283 Q_D(QProgressDialog);
284 d->init(labelText, cancelButtonText, minimum, maximum);
363void QProgressDialog::setCancelButton(QPushButton *cancelButton)
365 Q_D(QProgressDialog);
366 if (d->cancel == cancelButton) {
367 if (Q_UNLIKELY(cancelButton))
368 qWarning(
"QProgressDialog::setCancelButton: Attempt to set the same button again");
372 d->cancel = cancelButton;
374 connect(d->cancel, SIGNAL(clicked()),
this, SIGNAL(canceled()));
375#ifndef QT_NO_SHORTCUT
376 d->escapeShortcut =
new QShortcut(QKeySequence::Cancel,
this, SIGNAL(canceled()));
379#ifndef QT_NO_SHORTCUT
380 delete d->escapeShortcut;
381 d->escapeShortcut =
nullptr;
384 d->adoptChildWidget(cancelButton);
614void QProgressDialog::setValue(
int progress)
616 Q_D(QProgressDialog);
617 if (d->setValueCalled && progress == d->bar->value())
620 d->bar->setValue(progress);
623 if (isModal() && !d->processingEvents) {
624 const QScopedValueRollback guard(d->processingEvents,
true);
625 QCoreApplication::processEvents();
628 if ((!d->setValueCalled && progress == 0 ) || progress == minimum()) {
629 d->starttime.start();
630 d->forceTimer->start(d->showTime);
631 d->setValueCalled =
true;
634 d->setValueCalled =
true;
635 bool need_show =
false;
636 using namespace std::chrono;
637 nanoseconds elapsed = d->starttime.durationElapsed();
638 if (elapsed >= d->showTime) {
641 if (elapsed > minWaitTime) {
642 const int totalSteps = maximum() - minimum();
643 const int myprogress = std::max(progress - minimum(), 1);
644 const int remainingSteps = totalSteps - myprogress;
645 nanoseconds estimate;
646 if (remainingSteps >= INT_MAX / elapsed.count())
647 estimate = (remainingSteps / myprogress) * elapsed;
649 estimate = (elapsed * remainingSteps) / myprogress;
650 need_show = estimate >= d->showTime;
654 d->ensureSizeIsAtLeastSizeHint();
661 if (progress == d->bar->maximum() && d->autoReset)
671QSize QProgressDialog::sizeHint()
const
673 Q_D(
const QProgressDialog);
674 QSize labelSize = d->label ? d->label->sizeHint() : QSize(0, 0);
675 QSize barSize = d->bar->sizeHint();
676 int marginBottom = style()->pixelMetric(QStyle::PM_LayoutBottomMargin, 0,
this);
677 int spacing = style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing, 0,
this);
678 int marginLeft = style()->pixelMetric(QStyle::PM_LayoutLeftMargin, 0,
this);
679 int marginRight = style()->pixelMetric(QStyle::PM_LayoutRightMargin, 0,
this);
681 int height = marginBottom * 2 + barSize.height() + labelSize.height() + spacing;
683 height += d->cancel->sizeHint().height() + spacing;
684 return QSize(qMax(200, labelSize.width() + marginLeft + marginRight), height);
829void QProgressDialog::open(QObject *receiver,
const char *member)
831 Q_D(QProgressDialog);
832 connect(
this, SIGNAL(canceled()), receiver, member);
833 d->receiverToDisconnectOnClose = receiver;
834 d->memberToDisconnectOnClose = member;