Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qprogressdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qprogressdialog.h"
5
6#if QT_CONFIG(shortcut)
7# include "qshortcut.h"
8#endif
9#include "qpainter.h"
10#include "qdrawutil.h"
11#include "qlabel.h"
12#include "qprogressbar.h"
13#include "qapplication.h"
14#include "qstyle.h"
15#include "qpushbutton.h"
16#include "qtimer.h"
17#include "qelapsedtimer.h"
19#include <private/qdialog_p.h>
20
21#include <QtCore/qpointer.h>
22
23#include <limits.h>
24
25using namespace std::chrono_literals;
26
28
29// If the operation is expected to take this long (as predicted by
30// progress time), show the progress dialog.
31static constexpr auto defaultShowTime = 4000ms;
32// Wait at least this long before attempting to make a prediction.
33static constexpr auto minWaitTime = 50ms;
34
36{
37 Q_DECLARE_PUBLIC(QProgressDialog)
38
39public:
41
42 void init(const QString &labelText, const QString &cancelText, int min, int max);
43 void layout();
44 void retranslateStrings();
45 void setCancelButtonText(const QString &cancelButtonText);
49
50 QLabel *label = nullptr;
51 QPushButton *cancel = nullptr;
52 QProgressBar *bar = nullptr;
53 QTimer *forceTimer = nullptr;
54#ifndef QT_NO_SHORTCUT
56#endif
57 QPointer<QObject> receiverToDisconnectOnClose;
60 std::chrono::milliseconds showTime = defaultShowTime;
61 bool processingEvents = false;
62 bool shownOnce = false;
63 bool autoClose = true;
64 bool autoReset = true;
65 bool forceHide = false;
66 bool cancellationFlag = false;
67 bool setValueCalled = false;
69};
70
71void QProgressDialogPrivate::init(const QString &labelText, const QString &cancelText,
72 int min, int max)
73{
74 Q_Q(QProgressDialog);
75 label = new QLabel(labelText, q);
76 bar = new QProgressBar(q);
78 int align = q->style()->styleHint(QStyle::SH_ProgressDialog_TextLabelAlignment, nullptr, q);
79 label->setAlignment(Qt::Alignment(align));
80 QObject::connect(q, SIGNAL(canceled()), q, SLOT(cancel()));
81 forceTimer = new QTimer(q);
82 QObject::connect(forceTimer, SIGNAL(timeout()), q, SLOT(forceShow()));
85 } else {
86 q->setCancelButtonText(cancelText);
87 }
90}
91
93{
94 Q_Q(QProgressDialog);
95 int sp = q->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing, nullptr, q);
96 int mb = q->style()->pixelMetric(QStyle::PM_LayoutBottomMargin, nullptr, q);
97 int ml = qMin(q->width() / 10, q->style()->pixelMetric(QStyle::PM_LayoutLeftMargin, nullptr, q));
98 int mr = qMin(q->width() / 10, q->style()->pixelMetric(QStyle::PM_LayoutRightMargin, nullptr, q));
99 const bool centered =
100 bool(q->style()->styleHint(QStyle::SH_ProgressDialog_CenterCancelButton, nullptr, q));
101
102 int additionalSpacing = 0;
103 QSize cs = cancel ? cancel->sizeHint() : QSize(0,0);
104 QSize bh = bar->sizeHint();
105 int cspc;
106 int lh = 0;
107
108 // Find spacing and sizes that fit. It is important that a progress
109 // dialog can be made very small if the user demands it so.
110 for (int attempt=5; attempt--;) {
111 cspc = cancel ? cs.height() + sp : 0;
112 lh = qMax(0, q->height() - mb - bh.height() - sp - cspc);
113
114 if (lh < q->height()/4) {
115 // Getting cramped
116 sp /= 2;
117 mb /= 2;
118 if (cancel) {
119 cs.setHeight(qMax(4,cs.height()-sp-2));
120 }
121 bh.setHeight(qMax(4,bh.height()-sp-1));
122 } else {
123 break;
124 }
125 }
126
127 if (cancel) {
129 centered ? q->width()/2 - cs.width()/2 : q->width() - mr - cs.width(),
130 q->height() - mb - cs.height(),
131 cs.width(), cs.height());
132 }
133
134 if (label)
135 label->setGeometry(ml, additionalSpacing, q->width() - ml - mr, lh);
136 bar->setGeometry(ml, lh + sp + additionalSpacing, q->width() - ml - mr, bh.height());
137}
138
140{
142 setCancelButtonText(QProgressDialog::tr("Cancel"));
143}
144
155
247 : QDialog(*(new QProgressDialogPrivate), parent, f)
248{
249 Q_D(QProgressDialog);
250 d->useDefaultCancelText = true;
251 d->init(QString::fromLatin1(""), QString(), 0, 100);
252}
253
277 const QString &cancelButtonText,
278 int minimum, int maximum,
279 QWidget *parent, Qt::WindowFlags f)
280 : QDialog(*(new QProgressDialogPrivate), parent, f)
281{
282 Q_D(QProgressDialog);
283 d->init(labelText, cancelButtonText, minimum, maximum);
284}
285
286
294
314{
315 Q_D(QProgressDialog);
316 if (label == d->label) {
317 if (Q_UNLIKELY(label))
318 qWarning("QProgressDialog::setLabel: Attempt to set the same label again");
319 return;
320 }
321 delete d->label;
322 d->label = label;
323 d->adoptChildWidget(label);
324}
325
326
335{
336 Q_D(const QProgressDialog);
337 if (d->label)
338 return d->label->text();
339 return QString();
340}
341
343{
344 Q_D(QProgressDialog);
345 if (d->label) {
346 d->label->setText(text);
347 d->ensureSizeIsAtLeastSizeHint();
348 }
349}
350
351
363{
364 Q_D(QProgressDialog);
365 if (d->cancel == cancelButton) {
366 if (Q_UNLIKELY(cancelButton))
367 qWarning("QProgressDialog::setCancelButton: Attempt to set the same button again");
368 return;
369 }
370 delete d->cancel;
371 d->cancel = cancelButton;
372 if (cancelButton) {
373 connect(d->cancel, SIGNAL(clicked()), this, SIGNAL(canceled()));
374#ifndef QT_NO_SHORTCUT
375 d->escapeShortcut = new QShortcut(QKeySequence::Cancel, this, SIGNAL(canceled()));
376#endif
377 } else {
378#ifndef QT_NO_SHORTCUT
379 delete d->escapeShortcut;
380 d->escapeShortcut = nullptr;
381#endif
382 }
383 d->adoptChildWidget(cancelButton);
384}
385
395{
396 Q_D(QProgressDialog);
397 d->useDefaultCancelText = false;
398 d->setCancelButtonText(cancelButtonText);
399}
400
402{
403 Q_Q(QProgressDialog);
404
405 if (!cancelButtonText.isNull()) {
406 if (cancel) {
407 cancel->setText(cancelButtonText);
408 } else {
409 q->setCancelButton(new QPushButton(cancelButtonText, q));
410 }
411 } else {
412 q->setCancelButton(nullptr);
413 }
415}
416
417
426{
427 Q_D(QProgressDialog);
428 if (Q_UNLIKELY(!bar)) {
429 qWarning("QProgressDialog::setBar: Cannot set a null progress bar");
430 return;
431 }
432#ifndef QT_NO_DEBUG
433 if (Q_UNLIKELY(value() > 0))
434 qWarning("QProgressDialog::setBar: Cannot set a new progress bar "
435 "while the old one is active");
436#endif
437 if (Q_UNLIKELY(bar == d->bar)) {
438 qWarning("QProgressDialog::setBar: Attempt to set the same progress bar again");
439 return;
440 }
441 delete d->bar;
442 d->bar = bar;
443 d->adoptChildWidget(bar);
444}
445
447{
448 Q_Q(QProgressDialog);
449
450 if (c) {
451 if (c->parentWidget() == q)
452 c->hide(); // until after ensureSizeIsAtLeastSizeHint()
453 else
454 c->setParent(q, { });
455 }
457 //The layout should be updated again to prevent layout errors when the new 'widget' is replaced
458 layout();
459 if (c)
460 c->show();
461}
462
464{
465 Q_Q(QProgressDialog);
466
467 QSize size = q->sizeHint();
468 if (q->isVisible())
469 size = size.expandedTo(q->size());
470 q->resize(size);
471}
472
473
480{
481 Q_D(const QProgressDialog);
482 return d->cancellationFlag;
483}
484
485
496{
497 Q_D(const QProgressDialog);
498 return d->bar->maximum();
499}
500
502{
503 Q_D(QProgressDialog);
504 d->bar->setMaximum(maximum);
505}
506
517{
518 Q_D(const QProgressDialog);
519 return d->bar->minimum();
520}
521
523{
524 Q_D(QProgressDialog);
525 d->bar->setMinimum(minimum);
526}
527
540void QProgressDialog::setRange(int minimum, int maximum)
541{
542 Q_D(QProgressDialog);
543 d->bar->setRange(minimum, maximum);
544}
545
546
555{
556 Q_D(QProgressDialog);
557 if (d->autoClose || d->forceHide)
558 hide();
559 d->bar->reset();
560 d->cancellationFlag = false;
561 d->shownOnce = false;
562 d->setValueCalled = false;
563 d->forceTimer->stop();
564
565 /*
566 I wish we could disconnect the user slot provided to open() here but
567 unfortunately reset() is usually called before the slot has been invoked.
568 (reset() is itself invoked when canceled() is emitted.)
569 */
570 if (d->receiverToDisconnectOnClose)
571 QMetaObject::invokeMethod(this, "_q_disconnectOnClose", Qt::QueuedConnection);
572}
573
581{
582 Q_D(QProgressDialog);
583 d->forceHide = true;
584 reset();
585 d->forceHide = false;
586 d->cancellationFlag = true;
587}
588
589
591{
592 Q_D(const QProgressDialog);
593 return d->bar->value();
594}
595
614{
615 Q_D(QProgressDialog);
616 if (d->setValueCalled && progress == d->bar->value())
617 return;
618
619 d->bar->setValue(progress);
620
621 if (d->shownOnce) {
622 if (isModal() && !d->processingEvents) {
623 const QScopedValueRollback guard(d->processingEvents, true);
625 }
626 } else {
627 if ((!d->setValueCalled && progress == 0 /* for compat with Qt < 5.4 */) || progress == minimum()) {
628 d->starttime.start();
629 d->forceTimer->start(d->showTime);
630 d->setValueCalled = true;
631 return;
632 } else {
633 d->setValueCalled = true;
634 bool need_show = false;
635 using namespace std::chrono;
636 nanoseconds elapsed = d->starttime.durationElapsed();
637 if (elapsed >= d->showTime) {
638 need_show = true;
639 } else {
640 if (elapsed > minWaitTime) {
641 const int totalSteps = maximum() - minimum();
642 const int myprogress = std::max(progress - minimum(), 1);
643 const int remainingSteps = totalSteps - myprogress;
644 nanoseconds estimate;
645 if (remainingSteps >= INT_MAX / elapsed.count())
646 estimate = (remainingSteps / myprogress) * elapsed;
647 else
648 estimate = (elapsed * remainingSteps) / myprogress;
649 need_show = estimate >= d->showTime;
650 }
651 }
652 if (need_show) {
653 d->ensureSizeIsAtLeastSizeHint();
654 show();
655 d->shownOnce = true;
656 }
657 }
658 }
659
660 if (progress == d->bar->maximum() && d->autoReset)
661 reset();
662}
663
671{
672 Q_D(const QProgressDialog);
673 QSize labelSize = d->label ? d->label->sizeHint() : QSize(0, 0);
674 QSize barSize = d->bar->sizeHint();
675 int marginBottom = style()->pixelMetric(QStyle::PM_LayoutBottomMargin, 0, this);
677 int marginLeft = style()->pixelMetric(QStyle::PM_LayoutLeftMargin, 0, this);
678 int marginRight = style()->pixelMetric(QStyle::PM_LayoutRightMargin, 0, this);
679
680 int height = marginBottom * 2 + barSize.height() + labelSize.height() + spacing;
681 if (d->cancel)
682 height += d->cancel->sizeHint().height() + spacing;
683 return QSize(qMax(200, labelSize.width() + marginLeft + marginRight), height);
684}
685
689{
690 Q_D(QProgressDialog);
691 d->layout();
692}
693
698{
699 Q_D(QProgressDialog);
700 if (ev->type() == QEvent::StyleChange) {
701 d->layout();
702 } else if (ev->type() == QEvent::LanguageChange) {
703 d->retranslateStrings();
704 }
706}
707
723{
724 Q_D(QProgressDialog);
725 std::chrono::milliseconds msecs{ms};
726 d->showTime = msecs;
727 if (d->bar->value() == d->bar->minimum()) {
728 d->forceTimer->stop();
729 d->forceTimer->start(msecs);
730 }
731}
732
734{
735 Q_D(const QProgressDialog);
736 return int(d->showTime.count());
737}
738
739
749
760{
761 Q_D(QProgressDialog);
762 d->autoReset = b;
763}
764
766{
767 Q_D(const QProgressDialog);
768 return d->autoReset;
769}
770
781{
782 Q_D(QProgressDialog);
783 d->autoClose = close;
784}
785
787{
788 Q_D(const QProgressDialog);
789 return d->autoClose;
790}
791
797{
798 Q_D(QProgressDialog);
800 d->ensureSizeIsAtLeastSizeHint();
801 d->forceTimer->stop();
802}
803
812{
813 Q_D(QProgressDialog);
814 d->forceTimer->stop();
815 if (d->shownOnce || d->cancellationFlag)
816 return;
817
818 show();
819 d->shownOnce = true;
820}
821
830void QProgressDialog::open(QObject *receiver, const char *member)
831{
832 Q_D(QProgressDialog);
833 connect(this, SIGNAL(canceled()), receiver, member);
834 d->receiverToDisconnectOnClose = receiver;
835 d->memberToDisconnectOnClose = member;
837}
838
840
841#include "moc_qprogressdialog.cpp"
void setText(const QString &text)
\inmodule QtCore
Definition qbytearray.h:57
void clear()
Clears the contents of the byte array and makes it null.
The QCloseEvent class contains parameters that describe a close event.
Definition qevent.h:562
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Processes some pending events for the calling thread according to the specified flags.
The QDialog class is the base class of dialog windows.
Definition qdialog.h:19
void closeEvent(QCloseEvent *) override
\reimp
Definition qdialog.cpp:721
void showEvent(QShowEvent *) override
\reimp
Definition qdialog.cpp:853
virtual void open()
Definition qdialog.cpp:503
\inmodule QtCore
void start() noexcept
\typealias QElapsedTimer::Duration Synonym for std::chrono::nanoseconds.
\inmodule QtCore
Definition qcoreevent.h:45
@ StyleChange
Definition qcoreevent.h:136
@ LanguageChange
Definition qcoreevent.h:123
The QLabel widget provides a text or image display.
Definition qlabel.h:20
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
The QProgressBar widget provides a horizontal or vertical progress bar.
QSize sizeHint() const override
\reimp
void setRange(int minimum, int maximum)
Sets the progress bar's minimum and maximum values to minimum and maximum respectively.
void init(const QString &labelText, const QString &cancelText, int min, int max)
void setCancelButtonText(const QString &cancelButtonText)
std::chrono::milliseconds showTime
QPointer< QObject > receiverToDisconnectOnClose
QProgressDialogPrivate()=default
void adoptChildWidget(QWidget *c)
The QProgressDialog class provides feedback on the progress of a slow operation.
void showEvent(QShowEvent *event) override
\reimp
void setMinimum(int minimum)
void canceled()
This signal is emitted when the cancel button is clicked.
void setBar(QProgressBar *bar)
Sets the progress bar widget to bar.
void setValue(int progress)
int minimum
the lowest value represented by the progress bar
QString labelText
the label's text
void closeEvent(QCloseEvent *event) override
\reimp
void setMaximum(int maximum)
QProgressDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructs a progress dialog.
void setRange(int minimum, int maximum)
Sets the progress dialog's minimum and maximum values to minimum and maximum, respectively.
void changeEvent(QEvent *event) override
\reimp
bool wasCanceled
whether the dialog was canceled
bool autoClose
whether the dialog gets hidden by reset()
bool autoReset
whether the progress dialog calls reset() as soon as value() equals maximum().
void setAutoReset(bool reset)
void cancel()
Resets the progress dialog.
int minimumDuration
the time that must pass before the dialog appears
void setLabel(QLabel *label)
Sets the label to label.
QSize sizeHint() const override
Returns a size that fits the contents of the progress dialog.
int maximum
the highest value represented by the progress bar
void resizeEvent(QResizeEvent *event) override
\reimp
void forceShow()
Shows the dialog if it is still hidden after the algorithm has been started and minimumDuration milli...
int value
the current amount of progress made.
void reset()
Resets the progress dialog.
~QProgressDialog()
Destroys the progress dialog.
void setLabelText(const QString &text)
void setAutoClose(bool close)
virtual void open()
Definition qdialog.cpp:503
void setMinimumDuration(int ms)
void setCancelButton(QPushButton *button)
Sets the cancel button to the push button, cancelButton.
void setCancelButtonText(const QString &text)
Sets the cancel button's text to cancelButtonText.
The QPushButton widget provides a command button.
Definition qpushbutton.h:20
QSize sizeHint() const override
\reimp
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:548
The QShortcut class is used to create keyboard shortcuts.
Definition qshortcut.h:19
The QShowEvent class provides an event that is sent when a widget is shown.
Definition qevent.h:578
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
constexpr QSize expandedTo(const QSize &) const noexcept
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition qsize.h:192
constexpr void setHeight(int h) noexcept
Sets the height to the given height.
Definition qsize.h:139
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:994
@ SH_ProgressDialog_CenterCancelButton
Definition qstyle.h:594
@ SH_ProgressDialog_TextLabelAlignment
Definition qstyle.h:595
@ PM_LayoutBottomMargin
Definition qstyle.h:515
@ PM_LayoutLeftMargin
Definition qstyle.h:512
@ PM_LayoutVerticalSpacing
Definition qstyle.h:517
@ PM_LayoutRightMargin
Definition qstyle.h:514
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const =0
Returns the value of the given pixel metric.
\inmodule QtCore
Definition qtimer.h:20
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Definition qtimer.cpp:241
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:886
bool close()
Closes this widget.
Definition qwidget.cpp:8562
bool isModal() const
Definition qwidget.h:817
void hide()
Hides the widget.
Definition qwidget.cpp:8135
void show()
Shows the widget and its child widgets.
Definition qwidget.cpp:7875
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition qwidget.cpp:9382
QStyle * style() const
Definition qwidget.cpp:2600
QPushButton
[1]
QString text
qreal spacing
Combined button and popup list for selecting options.
@ QueuedConnection
#define Q_UNLIKELY(x)
#define qWarning
Definition qlogging.h:166
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLboolean GLboolean GLboolean b
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLbitfield GLuint64 timeout
[4]
GLfloat GLfloat f
GLuint GLsizei const GLchar * label
[43]
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
static QT_BEGIN_NAMESPACE constexpr auto defaultShowTime
static constexpr auto minWaitTime
#define sp
static double elapsed(qint64 after, qint64 before)
#define emit
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
\threadsafe This is an overloaded member function, provided for convenience. It differs from the abov...