Qt 6.x
The Qt SDK
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#include <limits.h>
21
22using namespace std::chrono_literals;
23
25
26// If the operation is expected to take this long (as predicted by
27// progress time), show the progress dialog.
28static constexpr auto defaultShowTime = 4000ms;
29// Wait at least this long before attempting to make a prediction.
30static constexpr auto minWaitTime = 50ms;
31
33{
34 Q_DECLARE_PUBLIC(QProgressDialog)
35
36public:
38
39 void init(const QString &labelText, const QString &cancelText, int min, int max);
40 void layout();
41 void retranslateStrings();
42 void setCancelButtonText(const QString &cancelButtonText);
46
47 QLabel *label = nullptr;
48 QPushButton *cancel = nullptr;
49 QProgressBar *bar = nullptr;
50 QTimer *forceTimer = nullptr;
51#ifndef QT_NO_SHORTCUT
53#endif
57 std::chrono::milliseconds showTime = defaultShowTime;
58 bool processingEvents = false;
59 bool shownOnce = false;
60 bool autoClose = true;
61 bool autoReset = true;
62 bool forceHide = false;
63 bool cancellationFlag = false;
64 bool setValueCalled = false;
66};
67
68void QProgressDialogPrivate::init(const QString &labelText, const QString &cancelText,
69 int min, int max)
70{
71 Q_Q(QProgressDialog);
72 label = new QLabel(labelText, q);
73 bar = new QProgressBar(q);
75 int align = q->style()->styleHint(QStyle::SH_ProgressDialog_TextLabelAlignment, nullptr, q);
76 label->setAlignment(Qt::Alignment(align));
77 QObject::connect(q, SIGNAL(canceled()), q, SLOT(cancel()));
78 forceTimer = new QTimer(q);
79 QObject::connect(forceTimer, SIGNAL(timeout()), q, SLOT(forceShow()));
82 } else {
83 q->setCancelButtonText(cancelText);
84 }
87}
88
90{
91 Q_Q(QProgressDialog);
92 int sp = q->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing, nullptr, q);
93 int mb = q->style()->pixelMetric(QStyle::PM_LayoutBottomMargin, nullptr, q);
94 int ml = qMin(q->width() / 10, q->style()->pixelMetric(QStyle::PM_LayoutLeftMargin, nullptr, q));
95 int mr = qMin(q->width() / 10, q->style()->pixelMetric(QStyle::PM_LayoutRightMargin, nullptr, q));
96 const bool centered =
97 bool(q->style()->styleHint(QStyle::SH_ProgressDialog_CenterCancelButton, nullptr, q));
98
99 int additionalSpacing = 0;
100 QSize cs = cancel ? cancel->sizeHint() : QSize(0,0);
101 QSize bh = bar->sizeHint();
102 int cspc;
103 int lh = 0;
104
105 // Find spacing and sizes that fit. It is important that a progress
106 // dialog can be made very small if the user demands it so.
107 for (int attempt=5; attempt--;) {
108 cspc = cancel ? cs.height() + sp : 0;
109 lh = qMax(0, q->height() - mb - bh.height() - sp - cspc);
110
111 if (lh < q->height()/4) {
112 // Getting cramped
113 sp /= 2;
114 mb /= 2;
115 if (cancel) {
116 cs.setHeight(qMax(4,cs.height()-sp-2));
117 }
118 bh.setHeight(qMax(4,bh.height()-sp-1));
119 } else {
120 break;
121 }
122 }
123
124 if (cancel) {
126 centered ? q->width()/2 - cs.width()/2 : q->width() - mr - cs.width(),
127 q->height() - mb - cs.height(),
128 cs.width(), cs.height());
129 }
130
131 if (label)
132 label->setGeometry(ml, additionalSpacing, q->width() - ml - mr, lh);
133 bar->setGeometry(ml, lh + sp + additionalSpacing, q->width() - ml - mr, bh.height());
134}
135
137{
139 setCancelButtonText(QProgressDialog::tr("Cancel"));
140}
141
143{
144 Q_Q(QProgressDialog);
149 }
151}
152
244{
245 Q_D(QProgressDialog);
246 d->useDefaultCancelText = true;
247 d->init(QString::fromLatin1(""), QString(), 0, 100);
248}
249
273 const QString &cancelButtonText,
274 int minimum, int maximum,
275 QWidget *parent, Qt::WindowFlags f)
277{
278 Q_D(QProgressDialog);
279 d->init(labelText, cancelButtonText, minimum, maximum);
280}
281
282
288{
289}
290
310{
311 Q_D(QProgressDialog);
312 if (label == d->label) {
313 if (Q_UNLIKELY(label))
314 qWarning("QProgressDialog::setLabel: Attempt to set the same label again");
315 return;
316 }
317 delete d->label;
318 d->label = label;
319 d->adoptChildWidget(label);
320}
321
322
331{
332 Q_D(const QProgressDialog);
333 if (d->label)
334 return d->label->text();
335 return QString();
336}
337
339{
340 Q_D(QProgressDialog);
341 if (d->label) {
342 d->label->setText(text);
343 d->ensureSizeIsAtLeastSizeHint();
344 }
345}
346
347
359{
360 Q_D(QProgressDialog);
361 if (d->cancel == cancelButton) {
362 if (Q_UNLIKELY(cancelButton))
363 qWarning("QProgressDialog::setCancelButton: Attempt to set the same button again");
364 return;
365 }
366 delete d->cancel;
367 d->cancel = cancelButton;
368 if (cancelButton) {
369 connect(d->cancel, SIGNAL(clicked()), this, SIGNAL(canceled()));
370#ifndef QT_NO_SHORTCUT
371 d->escapeShortcut = new QShortcut(QKeySequence::Cancel, this, SIGNAL(canceled()));
372#endif
373 } else {
374#ifndef QT_NO_SHORTCUT
375 delete d->escapeShortcut;
376 d->escapeShortcut = nullptr;
377#endif
378 }
379 d->adoptChildWidget(cancelButton);
380}
381
391{
392 Q_D(QProgressDialog);
393 d->useDefaultCancelText = false;
394 d->setCancelButtonText(cancelButtonText);
395}
396
398{
399 Q_Q(QProgressDialog);
400
401 if (!cancelButtonText.isNull()) {
402 if (cancel) {
403 cancel->setText(cancelButtonText);
404 } else {
405 q->setCancelButton(new QPushButton(cancelButtonText, q));
406 }
407 } else {
408 q->setCancelButton(nullptr);
409 }
411}
412
413
422{
423 Q_D(QProgressDialog);
424 if (Q_UNLIKELY(!bar)) {
425 qWarning("QProgressDialog::setBar: Cannot set a null progress bar");
426 return;
427 }
428#ifndef QT_NO_DEBUG
429 if (Q_UNLIKELY(value() > 0))
430 qWarning("QProgressDialog::setBar: Cannot set a new progress bar "
431 "while the old one is active");
432#endif
433 if (Q_UNLIKELY(bar == d->bar)) {
434 qWarning("QProgressDialog::setBar: Attempt to set the same progress bar again");
435 return;
436 }
437 delete d->bar;
438 d->bar = bar;
439 d->adoptChildWidget(bar);
440}
441
443{
444 Q_Q(QProgressDialog);
445
446 if (c) {
447 if (c->parentWidget() == q)
448 c->hide(); // until after ensureSizeIsAtLeastSizeHint()
449 else
450 c->setParent(q, { });
451 }
453 //The layout should be updated again to prevent layout errors when the new 'widget' is replaced
454 layout();
455 if (c)
456 c->show();
457}
458
460{
461 Q_Q(QProgressDialog);
462
463 QSize size = q->sizeHint();
464 if (q->isVisible())
465 size = size.expandedTo(q->size());
466 q->resize(size);
467}
468
469
476{
477 Q_D(const QProgressDialog);
478 return d->cancellationFlag;
479}
480
481
492{
493 Q_D(const QProgressDialog);
494 return d->bar->maximum();
495}
496
498{
499 Q_D(QProgressDialog);
500 d->bar->setMaximum(maximum);
501}
502
513{
514 Q_D(const QProgressDialog);
515 return d->bar->minimum();
516}
517
519{
520 Q_D(QProgressDialog);
521 d->bar->setMinimum(minimum);
522}
523
536void QProgressDialog::setRange(int minimum, int maximum)
537{
538 Q_D(QProgressDialog);
539 d->bar->setRange(minimum, maximum);
540}
541
542
551{
552 Q_D(QProgressDialog);
553 if (d->autoClose || d->forceHide)
554 hide();
555 d->bar->reset();
556 d->cancellationFlag = false;
557 d->shownOnce = false;
558 d->setValueCalled = false;
559 d->forceTimer->stop();
560
561 /*
562 I wish we could disconnect the user slot provided to open() here but
563 unfortunately reset() is usually called before the slot has been invoked.
564 (reset() is itself invoked when canceled() is emitted.)
565 */
566 if (d->receiverToDisconnectOnClose)
567 QMetaObject::invokeMethod(this, "_q_disconnectOnClose", Qt::QueuedConnection);
568}
569
577{
578 Q_D(QProgressDialog);
579 d->forceHide = true;
580 reset();
581 d->forceHide = false;
582 d->cancellationFlag = true;
583}
584
585
587{
588 Q_D(const QProgressDialog);
589 return d->bar->value();
590}
591
610{
611 Q_D(QProgressDialog);
612 if (d->setValueCalled && progress == d->bar->value())
613 return;
614
615 d->bar->setValue(progress);
616
617 if (d->shownOnce) {
618 if (isModal() && !d->processingEvents) {
619 const QScopedValueRollback guard(d->processingEvents, true);
621 }
622 } else {
623 if ((!d->setValueCalled && progress == 0 /* for compat with Qt < 5.4 */) || progress == minimum()) {
624 d->starttime.start();
625 d->forceTimer->start(d->showTime);
626 d->setValueCalled = true;
627 return;
628 } else {
629 d->setValueCalled = true;
630 bool need_show = false;
631 using namespace std::chrono;
632 nanoseconds elapsed = d->starttime.durationElapsed();
633 if (elapsed >= d->showTime) {
634 need_show = true;
635 } else {
636 if (elapsed > minWaitTime) {
637 const int totalSteps = maximum() - minimum();
638 const int myprogress = std::max(progress - minimum(), 1);
639 const int remainingSteps = totalSteps - myprogress;
640 nanoseconds estimate;
641 if (remainingSteps >= INT_MAX / elapsed.count())
642 estimate = (remainingSteps / myprogress) * elapsed;
643 else
644 estimate = (elapsed * remainingSteps) / myprogress;
645 need_show = estimate >= d->showTime;
646 }
647 }
648 if (need_show) {
649 d->ensureSizeIsAtLeastSizeHint();
650 show();
651 d->shownOnce = true;
652 }
653 }
654 }
655
656 if (progress == d->bar->maximum() && d->autoReset)
657 reset();
658}
659
667{
668 Q_D(const QProgressDialog);
669 QSize labelSize = d->label ? d->label->sizeHint() : QSize(0, 0);
670 QSize barSize = d->bar->sizeHint();
671 int marginBottom = style()->pixelMetric(QStyle::PM_LayoutBottomMargin, 0, this);
673 int marginLeft = style()->pixelMetric(QStyle::PM_LayoutLeftMargin, 0, this);
674 int marginRight = style()->pixelMetric(QStyle::PM_LayoutRightMargin, 0, this);
675
676 int height = marginBottom * 2 + barSize.height() + labelSize.height() + spacing;
677 if (d->cancel)
678 height += d->cancel->sizeHint().height() + spacing;
679 return QSize(qMax(200, labelSize.width() + marginLeft + marginRight), height);
680}
681
685{
686 Q_D(QProgressDialog);
687 d->layout();
688}
689
694{
695 Q_D(QProgressDialog);
696 if (ev->type() == QEvent::StyleChange) {
697 d->layout();
698 } else if (ev->type() == QEvent::LanguageChange) {
699 d->retranslateStrings();
700 }
702}
703
719{
720 Q_D(QProgressDialog);
721 std::chrono::milliseconds msecs{ms};
722 d->showTime = msecs;
723 if (d->bar->value() == d->bar->minimum()) {
724 d->forceTimer->stop();
725 d->forceTimer->start(msecs);
726 }
727}
728
730{
731 Q_D(const QProgressDialog);
732 return int(d->showTime.count());
733}
734
735
741{
742 emit canceled();
744}
745
756{
757 Q_D(QProgressDialog);
758 d->autoReset = b;
759}
760
762{
763 Q_D(const QProgressDialog);
764 return d->autoReset;
765}
766
777{
778 Q_D(QProgressDialog);
779 d->autoClose = close;
780}
781
783{
784 Q_D(const QProgressDialog);
785 return d->autoClose;
786}
787
793{
794 Q_D(QProgressDialog);
796 d->ensureSizeIsAtLeastSizeHint();
797 d->forceTimer->stop();
798}
799
808{
809 Q_D(QProgressDialog);
810 d->forceTimer->stop();
811 if (d->shownOnce || d->cancellationFlag)
812 return;
813
814 show();
815 d->shownOnce = true;
816}
817
826void QProgressDialog::open(QObject *receiver, const char *member)
827{
828 Q_D(QProgressDialog);
829 connect(this, SIGNAL(canceled()), receiver, member);
830 d->receiverToDisconnectOnClose = receiver;
831 d->memberToDisconnectOnClose = member;
833}
834
836
837#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:561
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:717
void showEvent(QShowEvent *) override
\reimp
Definition qdialog.cpp:850
virtual void open()
Definition qdialog.cpp:499
\inmodule QtCore
void start() noexcept
Starts this timer.
\inmodule QtCore
Definition qcoreevent.h:45
@ StyleChange
Definition qcoreevent.h:136
@ LanguageChange
Definition qcoreevent.h:123
Type type() const
Returns the event type.
Definition qcoreevent.h:299
The QLabel widget provides a text or image display.
Definition qlabel.h:20
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3099
\inmodule QtCore
Definition qpointer.h:18
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:499
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:547
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:577
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
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:191
constexpr void setHeight(int h) noexcept
Sets the height to the given height.
Definition qsize.h:138
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:898
@ SH_ProgressDialog_CenterCancelButton
Definition qstyle.h:592
@ SH_ProgressDialog_TextLabelAlignment
Definition qstyle.h:593
@ PM_LayoutBottomMargin
Definition qstyle.h:513
@ PM_LayoutLeftMargin
Definition qstyle.h:510
@ PM_LayoutVerticalSpacing
Definition qstyle.h:515
@ PM_LayoutRightMargin
Definition qstyle.h:512
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:208
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:8608
bool isModal() const
Definition qwidget.h:817
void hide()
Hides the widget.
Definition qwidget.cpp:8209
void show()
Shows the widget and its child widgets.
Definition qwidget.cpp:7956
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition qwidget.cpp:9428
QStyle * style() const
Definition qwidget.cpp:2607
QString text
qreal spacing
double e
Combined button and popup list for selecting options.
@ QueuedConnection
#define Q_UNLIKELY(x)
#define qWarning
Definition qlogging.h:162
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:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
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...
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent