Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
// Qt-Security score:significant reason:default
4
5
#
include
"qprogressdialog.h"
6
7
#
if
QT_CONFIG
(
shortcut
)
8
#
include
"qshortcut.h"
9
#
endif
10
#
include
"qpainter.h"
11
#
include
"qdrawutil.h"
12
#
include
"qlabel.h"
13
#
include
"qprogressbar.h"
14
#
include
"qapplication.h"
15
#
include
"qstyle.h"
16
#
include
"qpushbutton.h"
17
#
include
"qtimer.h"
18
#
include
"qelapsedtimer.h"
19
#
include
"qscopedvaluerollback.h"
20
#
include
<
private
/
qdialog_p
.
h
>
21
22
#
include
<
QtCore
/
qpointer
.
h
>
23
24
#
include
<
limits
.
h
>
25
26
using
namespace
std::chrono_literals;
27
28
QT_BEGIN_NAMESPACE
29
30
// If the operation is expected to take this long (as predicted by
31
// progress time), show the progress dialog.
32
static
constexpr
auto
defaultShowTime
= 4000ms;
33
// Wait at least this long before attempting to make a prediction.
34
static
constexpr
auto
minWaitTime
= 50ms;
35
36
class
QProgressDialogPrivate
:
public
QDialogPrivate
37
{
38
Q_DECLARE_PUBLIC(QProgressDialog)
39
40
public
:
41
QProgressDialogPrivate
() =
default
;
42
43
void
init
(
const
QString &labelText,
const
QString &cancelText,
int
min,
int
max);
44
void
layout
();
45
void
retranslateStrings
();
46
void
setCancelButtonText
(
const
QString &cancelButtonText);
47
void
adoptChildWidget
(
QWidget
*c);
48
void
ensureSizeIsAtLeastSizeHint
();
49
void
_q_disconnectOnClose
();
50
51
QLabel *
label
=
nullptr
;
52
QPushButton *
cancel
=
nullptr
;
53
QProgressBar *
bar
=
nullptr
;
54
QTimer *
forceTimer
=
nullptr
;
55
#
ifndef
QT_NO_SHORTCUT
56
QShortcut
*
escapeShortcut
=
nullptr
;
57
#
endif
58
QPointer
<
QObject
>
receiverToDisconnectOnClose
;
59
QElapsedTimer
starttime
;
60
QByteArray
memberToDisconnectOnClose
;
61
std::chrono::milliseconds
showTime
=
defaultShowTime
;
62
bool
processingEvents
=
false
;
63
bool
shownOnce
=
false
;
64
bool
autoClose
=
true
;
65
bool
autoReset
=
true
;
66
bool
forceHide
=
false
;
67
bool
cancellationFlag
=
false
;
68
bool
setValueCalled
=
false
;
69
bool
useDefaultCancelText
=
false
;
70
};
71
72
void
QProgressDialogPrivate
::
init
(
const
QString &labelText,
const
QString &cancelText,
73
int
min,
int
max)
74
{
75
Q_Q(QProgressDialog);
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()));
84
if
(
useDefaultCancelText
) {
85
retranslateStrings
(
)
;
86
}
else
{
87
q->setCancelButtonText(cancelText);
88
}
89
starttime.start();
90
forceTimer
->start(showTime);
91
}
92
93
void
QProgressDialogPrivate
::
layout
()
94
{
95
Q_Q(QProgressDialog);
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));
102
103
int
additionalSpacing = 0;
104
QSize cs =
cancel
?
cancel
->sizeHint() : QSize(0,0);
105
QSize bh =
bar
->sizeHint();
106
int
cspc;
107
int
lh = 0;
108
109
// Find spacing and sizes that fit. It is important that a progress
110
// dialog can be made very small if the user demands it so.
111
for
(
int
attempt=5; attempt--;) {
112
cspc =
cancel
? cs.height() + sp : 0;
113
lh = qMax(0, q->height() - mb - bh.height() - sp - cspc);
114
115
if
(lh < q->height()/4) {
116
// Getting cramped
117
sp /= 2;
118
mb /= 2;
119
if
(
cancel
) {
120
cs.setHeight(qMax(4,cs.height()-sp-2));
121
}
122
bh.setHeight(qMax(4,bh.height()-sp-1));
123
}
else
{
124
break
;
125
}
126
}
127
128
if
(
cancel
) {
129
cancel
->setGeometry(
130
centered ? q->width()/2 - cs.width()/2 : q->width() - mr - cs.width(),
131
q->height() - mb - cs.height(),
132
cs.width(), cs.height());
133
}
134
135
if
(
label
)
136
label
->setGeometry(ml, additionalSpacing, q->width() - ml - mr, lh);
137
bar
->setGeometry(ml, lh + sp + additionalSpacing, q->width() - ml - mr, bh.height());
138
}
139
140
void
QProgressDialogPrivate
::
retranslateStrings
()
141
{
142
if
(useDefaultCancelText)
143
setCancelButtonText(QProgressDialog::tr(
"Cancel"
));
144
}
145
146
void
QProgressDialogPrivate
::
_q_disconnectOnClose
()
147
{
148
Q_Q(QProgressDialog);
149
if
(receiverToDisconnectOnClose) {
150
QObject::disconnect(q, SIGNAL(canceled()), receiverToDisconnectOnClose,
151
memberToDisconnectOnClose);
152
receiverToDisconnectOnClose =
nullptr
;
153
}
154
memberToDisconnectOnClose.clear();
155
}
156
157
/*!
158
\class QProgressDialog
159
\brief The QProgressDialog class provides feedback on the progress of a slow operation.
160
\ingroup standard-dialogs
161
\inmodule QtWidgets
162
163
164
A progress dialog is used to give the user an indication of how long
165
an operation is going to take, and to demonstrate that the
166
application has not frozen. It can also give the user an opportunity
167
to abort the operation.
168
169
A common problem with progress dialogs is that it is difficult to know
170
when to use them; operations take different amounts of time on different
171
hardware. QProgressDialog offers a solution to this problem:
172
it estimates the time the operation will take (based on time for
173
steps), and only shows itself if that estimate is beyond minimumDuration()
174
(4 seconds by default).
175
176
Use setMinimum() and setMaximum() or the constructor to set the number of
177
"steps" in the operation and call setValue() as the operation
178
progresses. The number of steps can be chosen arbitrarily. It can be the
179
number of files copied, the number of bytes received, the number of
180
iterations through the main loop of your algorithm, or some other
181
suitable unit. Progress starts at the value set by setMinimum(),
182
and the progress dialog shows that the operation has finished when
183
you call setValue() with the value set by setMaximum() as its argument.
184
185
The dialog automatically resets and hides itself at the end of the
186
operation. Use setAutoReset() and setAutoClose() to change this
187
behavior. Note that if you set a new maximum (using setMaximum() or
188
setRange()) that equals your current value(), the dialog will not
189
close regardless.
190
191
There are two ways of using QProgressDialog: modal and modeless.
192
193
Compared to a modeless QProgressDialog, a modal QProgressDialog is simpler
194
to use for the programmer. Do the operation in a loop, call \l setValue() at
195
intervals, and check for cancellation with wasCanceled(). For example:
196
197
\snippet dialogs/dialogs.cpp 3
198
199
A modeless progress dialog is suitable for operations that take
200
place in the background, where the user is able to interact with the
201
application. Such operations are typically based on a timer class,
202
such as QChronoTimer (or the more low-level QObject::timerEvent()) or
203
QSocketNotifier; or performed in a separate thread. A QProgressBar in
204
the status bar of your main window is often an alternative to a modeless
205
progress dialog.
206
207
You need to have an event loop to be running, connect the
208
canceled() signal to a slot that stops the operation, and call \l
209
setValue() at intervals. For example:
210
211
\snippet dialogs/dialogs.cpp 4
212
\codeline
213
\snippet dialogs/dialogs.cpp 5
214
\codeline
215
\snippet dialogs/dialogs.cpp 6
216
217
In both modes the progress dialog may be customized by
218
replacing the child widgets with custom widgets by using setLabel(),
219
setBar(), and setCancelButton().
220
The functions setLabelText() and setCancelButtonText()
221
set the texts shown.
222
223
\image fusion-progressdialog.png A progress dialog shown in the Fusion widget style.
224
225
\sa QDialog, QProgressBar
226
*/
227
228
229
/*!
230
Constructs a progress dialog.
231
232
Default settings:
233
\list
234
\li The label text is empty.
235
\li The cancel button text is (translated) "Cancel".
236
\li minimum is 0;
237
\li maximum is 100
238
\endlist
239
240
The \a parent argument is dialog's parent widget. The widget flags, \a f, are
241
passed to the QDialog::QDialog() constructor.
242
243
\sa setLabelText(), setCancelButtonText(), setCancelButton(),
244
setMinimum(), setMaximum()
245
*/
246
247
QProgressDialog::QProgressDialog(QWidget *parent, Qt::WindowFlags f)
248
: QDialog(*(
new
QProgressDialogPrivate), parent, f)
249
{
250
Q_D(QProgressDialog);
251
d->useDefaultCancelText =
true
;
252
d->init(QString::fromLatin1(
""
), QString(), 0, 100);
253
}
254
255
/*!
256
Constructs a progress dialog.
257
258
The \a labelText is the text used to remind the user what is progressing.
259
260
The \a cancelButtonText is the text to display on the cancel button. If
261
QString() is passed then no cancel button is shown.
262
263
The \a minimum and \a maximum is the number of steps in the operation for
264
which this progress dialog shows progress. For example, if the
265
operation is to examine 50 files, this value minimum value would be 0,
266
and the maximum would be 50. Before examining the first file, call
267
setValue(0). As each file is processed call setValue(1), setValue(2),
268
etc., finally calling setValue(50) after examining the last file.
269
270
The \a parent argument is the dialog's parent widget. The parent, \a parent, and
271
widget flags, \a f, are passed to the QDialog::QDialog() constructor.
272
273
\sa setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(),
274
setMinimum(), setMaximum()
275
*/
276
277
QProgressDialog::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)
282
{
283
Q_D(QProgressDialog);
284
d->init(labelText, cancelButtonText, minimum, maximum);
285
}
286
287
288
/*!
289
Destroys the progress dialog.
290
*/
291
292
QProgressDialog::~QProgressDialog()
293
{
294
}
295
296
/*!
297
\fn void QProgressDialog::canceled()
298
299
This signal is emitted when the cancel button is clicked.
300
It is connected to the cancel() slot by default.
301
302
\sa wasCanceled()
303
*/
304
305
306
/*!
307
Sets the label to \a label. The progress dialog resizes to fit. The
308
label becomes owned by the progress dialog and will be deleted when
309
necessary, so do not pass the address of an object on the stack.
310
311
\sa setLabelText()
312
*/
313
314
void
QProgressDialog::setLabel(QLabel *label)
315
{
316
Q_D(QProgressDialog);
317
if
(label == d->label) {
318
if
(Q_UNLIKELY(label))
319
qWarning(
"QProgressDialog::setLabel: Attempt to set the same label again"
);
320
return
;
321
}
322
delete
d->label;
323
d->label = label;
324
d->adoptChildWidget(label);
325
}
326
327
328
/*!
329
\property QProgressDialog::labelText
330
\brief the label's text
331
332
The default text is an empty string.
333
*/
334
335
QString QProgressDialog::labelText()
const
336
{
337
Q_D(
const
QProgressDialog);
338
if
(d->label)
339
return
d->label->text();
340
return
QString();
341
}
342
343
void
QProgressDialog::setLabelText(
const
QString &text)
344
{
345
Q_D(QProgressDialog);
346
if
(d->label) {
347
d->label->setText(text);
348
d->ensureSizeIsAtLeastSizeHint();
349
}
350
}
351
352
353
/*!
354
Sets the cancel button to the push button, \a cancelButton. The
355
progress dialog takes ownership of this button which will be deleted
356
when necessary, so do not pass the address of an object that is on
357
the stack, i.e. use new() to create the button. If \nullptr is passed,
358
no cancel button will be shown.
359
360
\sa setCancelButtonText()
361
*/
362
363
void
QProgressDialog::setCancelButton(QPushButton *cancelButton)
364
{
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"
);
369
return
;
370
}
371
delete
d->cancel;
372
d->cancel = cancelButton;
373
if
(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()));
377
#
endif
378
}
else
{
379
#
ifndef
QT_NO_SHORTCUT
380
delete
d->escapeShortcut;
381
d->escapeShortcut =
nullptr
;
382
#
endif
383
}
384
d->adoptChildWidget(cancelButton);
385
}
386
387
/*!
388
Sets the cancel button's text to \a cancelButtonText. If the text
389
is set to QString() then it will cause the cancel button to be
390
hidden and deleted.
391
392
\sa setCancelButton()
393
*/
394
395
void
QProgressDialog::setCancelButtonText(
const
QString &cancelButtonText)
396
{
397
Q_D(QProgressDialog);
398
d->useDefaultCancelText =
false
;
399
d->setCancelButtonText(cancelButtonText);
400
}
401
402
void
QProgressDialogPrivate
::
setCancelButtonText
(
const
QString &cancelButtonText)
403
{
404
Q_Q(QProgressDialog);
405
406
if
(!cancelButtonText.isNull()) {
407
if
(
cancel
) {
408
cancel
->setText(cancelButtonText);
409
}
else
{
410
q->setCancelButton(
new
QPushButton(cancelButtonText, q));
411
}
412
}
else
{
413
q->setCancelButton(
nullptr
);
414
}
415
ensureSizeIsAtLeastSizeHint
(
)
;
416
}
417
418
419
/*!
420
Sets the progress bar widget to \a bar. The progress dialog resizes to
421
fit. The progress dialog takes ownership of the progress \a bar which
422
will be deleted when necessary, so do not use a progress bar
423
allocated on the stack.
424
*/
425
426
void
QProgressDialog::setBar(QProgressBar *bar)
427
{
428
Q_D(QProgressDialog);
429
if
(Q_UNLIKELY(!bar)) {
430
qWarning(
"QProgressDialog::setBar: Cannot set a null progress bar"
);
431
return
;
432
}
433
#
ifndef
QT_NO_DEBUG
434
if
(Q_UNLIKELY(value() > 0))
435
qWarning(
"QProgressDialog::setBar: Cannot set a new progress bar "
436
"while the old one is active"
);
437
#
endif
438
if
(Q_UNLIKELY(bar == d->bar)) {
439
qWarning(
"QProgressDialog::setBar: Attempt to set the same progress bar again"
);
440
return
;
441
}
442
delete
d->bar;
443
d->bar = bar;
444
d->adoptChildWidget(bar);
445
}
446
447
void
QProgressDialogPrivate
::
adoptChildWidget
(
QWidget
*c)
448
{
449
Q_Q(QProgressDialog);
450
451
if
(c) {
452
if
(c->parentWidget() == q)
453
c->hide();
// until after ensureSizeIsAtLeastSizeHint()
454
else
455
c->setParent(q, { });
456
}
457
ensureSizeIsAtLeastSizeHint
(
)
;
458
//The layout should be updated again to prevent layout errors when the new 'widget' is replaced
459
layout
(
)
;
460
if
(c)
461
c->show();
462
}
463
464
void
QProgressDialogPrivate
::
ensureSizeIsAtLeastSizeHint
()
465
{
466
Q_Q(QProgressDialog);
467
468
QSize size = q->sizeHint();
469
if
(q->isVisible())
470
size = size.expandedTo(q->size());
471
q->resize(size);
472
}
473
474
475
/*!
476
\property QProgressDialog::wasCanceled
477
\brief whether the dialog was canceled
478
*/
479
480
bool
QProgressDialog::wasCanceled()
const
481
{
482
Q_D(
const
QProgressDialog);
483
return
d->cancellationFlag;
484
}
485
486
487
/*!
488
\property QProgressDialog::maximum
489
\brief the highest value represented by the progress bar
490
491
The default is 100.
492
493
\sa minimum, setRange()
494
*/
495
496
int
QProgressDialog::maximum()
const
497
{
498
Q_D(
const
QProgressDialog);
499
return
d->bar->maximum();
500
}
501
502
void
QProgressDialog::setMaximum(
int
maximum)
503
{
504
Q_D(QProgressDialog);
505
d->bar->setMaximum(maximum);
506
}
507
508
/*!
509
\property QProgressDialog::minimum
510
\brief the lowest value represented by the progress bar
511
512
The default is 0.
513
514
\sa maximum, setRange()
515
*/
516
517
int
QProgressDialog::minimum()
const
518
{
519
Q_D(
const
QProgressDialog);
520
return
d->bar->minimum();
521
}
522
523
void
QProgressDialog::setMinimum(
int
minimum)
524
{
525
Q_D(QProgressDialog);
526
d->bar->setMinimum(minimum);
527
}
528
529
/*!
530
Sets the progress dialog's minimum and maximum values
531
to \a minimum and \a maximum, respectively.
532
533
If \a maximum is smaller than \a minimum, \a minimum becomes the only
534
legal value.
535
536
If the current value falls outside the new range, the progress
537
dialog is reset with reset().
538
539
\sa minimum, maximum
540
*/
541
void
QProgressDialog::setRange(
int
minimum,
int
maximum)
542
{
543
Q_D(QProgressDialog);
544
d->bar->setRange(minimum, maximum);
545
}
546
547
548
/*!
549
Resets the progress dialog.
550
The progress dialog becomes hidden if autoClose() is true.
551
552
\sa setAutoClose(), setAutoReset()
553
*/
554
555
void
QProgressDialog::reset()
556
{
557
Q_D(QProgressDialog);
558
if
(d->autoClose || d->forceHide)
559
hide();
560
d->bar->reset();
561
d->cancellationFlag =
false
;
562
d->shownOnce =
false
;
563
d->setValueCalled =
false
;
564
d->forceTimer->stop();
565
566
/*
567
I wish we could disconnect the user slot provided to open() here but
568
unfortunately reset() is usually called before the slot has been invoked.
569
(reset() is itself invoked when canceled() is emitted.)
570
*/
571
if
(d->receiverToDisconnectOnClose)
572
QMetaObject::invokeMethod(
this
,
"_q_disconnectOnClose"
, Qt::QueuedConnection);
573
}
574
575
/*!
576
Resets the progress dialog. wasCanceled() becomes true until
577
the progress dialog is reset.
578
The progress dialog becomes hidden.
579
*/
580
581
void
QProgressDialog::cancel()
582
{
583
Q_D(QProgressDialog);
584
d->forceHide =
true
;
585
reset();
586
d->forceHide =
false
;
587
d->cancellationFlag =
true
;
588
}
589
590
591
int
QProgressDialog::value()
const
592
{
593
Q_D(
const
QProgressDialog);
594
return
d->bar->value();
595
}
596
597
/*!
598
\property QProgressDialog::value
599
\brief the current amount of progress made.
600
601
For the progress dialog to work as expected, you should initially set
602
this property to QProgressDialog::minimum() and finally set it to
603
QProgressDialog::maximum(); you can call setValue() any number of times
604
in-between.
605
606
\warning If the progress dialog is modal
607
(see QProgressDialog::QProgressDialog()),
608
setValue() calls QCoreApplication::processEvents(), so take care that
609
this does not cause undesirable re-entrancy in your code. For example,
610
don't use a QProgressDialog inside a paintEvent()!
611
612
\sa minimum, maximum
613
*/
614
void
QProgressDialog::setValue(
int
progress)
615
{
616
Q_D(QProgressDialog);
617
if
(d->setValueCalled && progress == d->bar->value())
618
return
;
619
620
d->bar->setValue(progress);
621
622
if
(d->shownOnce) {
623
if
(isModal() && !d->processingEvents) {
624
const
QScopedValueRollback guard(d->processingEvents,
true
);
625
QCoreApplication::processEvents();
626
}
627
}
else
{
628
if
((!d->setValueCalled && progress == 0
/* for compat with Qt < 5.4 */
) || progress == minimum()) {
629
d->starttime.start();
630
d->forceTimer->start(d->showTime);
631
d->setValueCalled =
true
;
632
return
;
633
}
else
{
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) {
639
need_show =
true
;
640
}
else
{
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;
648
else
649
estimate = (elapsed * remainingSteps) / myprogress;
650
need_show = estimate >= d->showTime;
651
}
652
}
653
if
(need_show) {
654
d->ensureSizeIsAtLeastSizeHint();
655
show();
656
d->shownOnce =
true
;
657
}
658
}
659
}
660
661
if
(progress == d->bar->maximum() && d->autoReset)
662
reset();
663
}
664
665
/*!
666
Returns a size that fits the contents of the progress dialog.
667
The progress dialog resizes itself as required, so you should not
668
need to call this yourself.
669
*/
670
671
QSize QProgressDialog::sizeHint()
const
672
{
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
);
680
681
int
height = marginBottom * 2 + barSize.height() + labelSize.height() + spacing;
682
if
(d->cancel)
683
height += d->cancel->sizeHint().height() + spacing;
684
return
QSize(qMax(200, labelSize.width() + marginLeft + marginRight), height);
685
}
686
687
/*!\reimp
688
*/
689
void
QProgressDialog::resizeEvent(QResizeEvent *)
690
{
691
Q_D(QProgressDialog);
692
d->layout();
693
}
694
695
/*!
696
\reimp
697
*/
698
void
QProgressDialog::changeEvent(QEvent *ev)
699
{
700
Q_D(QProgressDialog);
701
if
(ev->type() == QEvent::StyleChange) {
702
d->layout();
703
}
else
if
(ev->type() == QEvent::LanguageChange) {
704
d->retranslateStrings();
705
}
706
QDialog::changeEvent(ev);
707
}
708
709
/*!
710
\property QProgressDialog::minimumDuration
711
\brief the time that must pass before the dialog appears
712
713
If the expected duration of the task is less than the
714
minimumDuration, the dialog will not appear at all. This prevents
715
the dialog popping up for tasks that are quickly over. For tasks
716
that are expected to exceed the minimumDuration, the dialog will
717
pop up after the minimumDuration time or as soon as any progress
718
is set.
719
720
If set to 0, the dialog is always shown as soon as any progress is
721
set. The default is 4000 milliseconds.
722
*/
723
void
QProgressDialog::setMinimumDuration(
int
ms)
724
{
725
Q_D(QProgressDialog);
726
std::chrono::milliseconds msecs{ms};
727
d->showTime = msecs;
728
if
(d->setValueCalled && d->bar->value() == d->bar->minimum()) {
729
d->forceTimer->stop();
730
d->forceTimer->start(msecs);
731
}
732
}
733
734
int
QProgressDialog::minimumDuration()
const
735
{
736
Q_D(
const
QProgressDialog);
737
return
int
(d->showTime.count());
738
}
739
740
741
/*!
742
\reimp
743
*/
744
745
void
QProgressDialog::closeEvent(QCloseEvent *e)
746
{
747
emit canceled();
748
QDialog::closeEvent(e);
749
}
750
751
/*!
752
\property QProgressDialog::autoReset
753
\brief whether the progress dialog calls reset() as soon as value() equals maximum().
754
755
The default is true.
756
757
\sa setAutoClose()
758
*/
759
760
void
QProgressDialog::setAutoReset(
bool
b)
761
{
762
Q_D(QProgressDialog);
763
d->autoReset = b;
764
}
765
766
bool
QProgressDialog::autoReset()
const
767
{
768
Q_D(
const
QProgressDialog);
769
return
d->autoReset;
770
}
771
772
/*!
773
\property QProgressDialog::autoClose
774
\brief whether the dialog gets hidden by reset()
775
776
The default is true.
777
778
\sa setAutoReset()
779
*/
780
781
void
QProgressDialog::setAutoClose(
bool
close)
782
{
783
Q_D(QProgressDialog);
784
d->autoClose = close;
785
}
786
787
bool
QProgressDialog::autoClose()
const
788
{
789
Q_D(
const
QProgressDialog);
790
return
d->autoClose;
791
}
792
793
/*!
794
\reimp
795
*/
796
797
void
QProgressDialog::showEvent(QShowEvent *e)
798
{
799
Q_D(QProgressDialog);
800
QDialog::showEvent(e);
801
d->ensureSizeIsAtLeastSizeHint();
802
d->forceTimer->stop();
803
}
804
805
/*!
806
\reimp
807
*/
808
void
QProgressDialog::setVisible(
bool
visible)
809
{
810
Q_D(QProgressDialog);
811
if
(!visible) {
812
d->forceTimer->stop();
813
// let the next setValue(minimum()) restart the timer, even with an unchanged value
814
d->setValueCalled =
false
;
815
}
816
QDialog::setVisible(visible);
817
}
818
819
/*!
820
Shows the dialog if it is still hidden after the algorithm has been started
821
and minimumDuration milliseconds have passed.
822
823
\sa setMinimumDuration()
824
*/
825
826
void
QProgressDialog::forceShow()
827
{
828
Q_D(QProgressDialog);
829
d->forceTimer->stop();
830
if
(d->shownOnce || d->cancellationFlag)
831
return
;
832
833
show();
834
d->shownOnce =
true
;
835
}
836
837
/*!
838
Opens the dialog and connects its canceled() signal to the slot specified
839
by \a receiver and \a member.
840
841
The signal will be disconnected from the slot when the dialog is closed.
842
*/
843
void
QProgressDialog::open(QObject *receiver,
const
char
*member)
844
{
845
Q_D(QProgressDialog);
846
connect(
this
, SIGNAL(canceled()), receiver, member);
847
d->receiverToDisconnectOnClose = receiver;
848
d->memberToDisconnectOnClose = member;
849
QDialog::open();
850
}
851
852
QT_END_NAMESPACE
853
854
#
include
"moc_qprogressdialog.cpp"
QPainter::QWidget
friend class QWidget
Definition
qpainter.h:432
QProgressDialogPrivate
Definition
qprogressdialog.cpp:37
QProgressDialogPrivate::autoReset
bool autoReset
Definition
qprogressdialog.cpp:65
QProgressDialogPrivate::cancel
QPushButton * cancel
Definition
qprogressdialog.cpp:52
QProgressDialogPrivate::memberToDisconnectOnClose
QByteArray memberToDisconnectOnClose
Definition
qprogressdialog.cpp:60
QProgressDialogPrivate::layout
void layout()
Definition
qprogressdialog.cpp:93
QProgressDialogPrivate::escapeShortcut
QShortcut * escapeShortcut
Definition
qprogressdialog.cpp:56
QProgressDialogPrivate::label
QLabel * label
Definition
qprogressdialog.cpp:51
QProgressDialogPrivate::forceTimer
QTimer * forceTimer
Definition
qprogressdialog.cpp:54
QProgressDialogPrivate::retranslateStrings
void retranslateStrings()
Definition
qprogressdialog.cpp:140
QProgressDialogPrivate::init
void init(const QString &labelText, const QString &cancelText, int min, int max)
Definition
qprogressdialog.cpp:72
QProgressDialogPrivate::setCancelButtonText
void setCancelButtonText(const QString &cancelButtonText)
Definition
qprogressdialog.cpp:402
QProgressDialogPrivate::showTime
std::chrono::milliseconds showTime
Definition
qprogressdialog.cpp:61
QProgressDialogPrivate::processingEvents
bool processingEvents
Definition
qprogressdialog.cpp:62
QProgressDialogPrivate::setValueCalled
bool setValueCalled
Definition
qprogressdialog.cpp:68
QProgressDialogPrivate::forceHide
bool forceHide
Definition
qprogressdialog.cpp:66
QProgressDialogPrivate::cancellationFlag
bool cancellationFlag
Definition
qprogressdialog.cpp:67
QProgressDialogPrivate::ensureSizeIsAtLeastSizeHint
void ensureSizeIsAtLeastSizeHint()
Definition
qprogressdialog.cpp:464
QProgressDialogPrivate::autoClose
bool autoClose
Definition
qprogressdialog.cpp:64
QProgressDialogPrivate::shownOnce
bool shownOnce
Definition
qprogressdialog.cpp:63
QProgressDialogPrivate::_q_disconnectOnClose
void _q_disconnectOnClose()
Definition
qprogressdialog.cpp:146
QProgressDialogPrivate::useDefaultCancelText
bool useDefaultCancelText
Definition
qprogressdialog.cpp:69
QProgressDialogPrivate::adoptChildWidget
void adoptChildWidget(QWidget *c)
Definition
qprogressdialog.cpp:447
QProgressDialogPrivate::receiverToDisconnectOnClose
QPointer< QObject > receiverToDisconnectOnClose
Definition
qprogressdialog.cpp:58
QProgressDialogPrivate::starttime
QElapsedTimer starttime
Definition
qprogressdialog.cpp:59
QProgressDialogPrivate::bar
QProgressBar * bar
Definition
qprogressdialog.cpp:53
defaultShowTime
static QT_BEGIN_NAMESPACE constexpr auto defaultShowTime
Definition
qprogressdialog.cpp:32
minWaitTime
static constexpr auto minWaitTime
Definition
qprogressdialog.cpp:34
qtbase
src
widgets
dialogs
qprogressdialog.cpp
Generated on
for Qt by
1.18.0