QProgressDialog Class
The QProgressDialog class provides feedback on the progress of a slow operation. More...
| Header: | #include <QProgressDialog> |
| qmake: | QT += widgets |
| Inherits: | QDialog |
Properties
|
Public Functions
| bool | autoClose() const |
| bool | autoReset() const |
| QString | labelText() const |
| int | maximum() const |
| int | minimum() const |
| int | minimumDuration() const |
| void | setAutoClose(bool close) |
| void | setAutoReset(bool reset) |
| int | value() const |
| bool | wasCanceled() const |
Public Slots
| void | setLabelText(const QString &text) |
| void | setMaximum(int maximum) |
| void | setMinimum(int minimum) |
| void | setMinimumDuration(int ms) |
| void | setValue(int progress) |
Detailed Description
A progress dialog is used to give the user an indication of how long an operation is going to take, and to demonstrate that the application has not frozen. It can also give the user an opportunity to abort the operation.
A common problem with progress dialogs is that it is difficult to know when to use them; operations take different amounts of time on different hardware. QProgressDialog offers a solution to this problem: it estimates the time the operation will take (based on time for steps), and only shows itself if that estimate is beyond minimumDuration() (4 seconds by default).
Use setMinimum() and setMaximum() or the constructor to set the number of "steps" in the operation and call setValue() as the operation progresses. The number of steps can be chosen arbitrarily. It can be the number of files copied, the number of bytes received, the number of iterations through the main loop of your algorithm, or some other suitable unit. Progress starts at the value set by setMinimum(), and the progress dialog shows that the operation has finished when you call setValue() with the value set by setMaximum() as its argument.
The dialog automatically resets and hides itself at the end of the operation. Use setAutoReset() and setAutoClose() to change this behavior. Note that if you set a new maximum (using setMaximum() or setRange()) that equals your current value(), the dialog will not close regardless.
There are two ways of using QProgressDialog: modal and modeless.
Compared to a modeless QProgressDialog, a modal QProgressDialog is simpler to use for the programmer. Do the operation in a loop, call setValue() at intervals, and check for cancellation with wasCanceled(). For example:
QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
progress.setWindowModality(Qt::WindowModal);
for (int i = 0; i < numFiles; i++) {
progress.setValue(i);
if (progress.wasCanceled())
break;
//... copy one file
}
progress.setValue(numFiles);
A modeless progress dialog is suitable for operations that take place in the background, where the user is able to interact with the application. Such operations are typically based on QTimer (or QObject::timerEvent()) or QSocketNotifier; or performed in a separate thread. A QProgressBar in the status bar of your main window is often an alternative to a modeless progress dialog.
You need to have an event loop to be running, connect the canceled() signal to a slot that stops the operation, and call setValue() at intervals. For example:
// Operation constructor Operation::Operation(QObject *parent) : QObject(parent), steps(0) { pd = new QProgressDialog("Operation in progress.", "Cancel", 0, 100); connect(pd, &QProgressDialog::canceled, this, &Operation::cancel); t = new QTimer(this); connect(t, &QTimer::timeout, this, &Operation::perform); t->start(0); } void Operation::perform() { pd->setValue(steps); //... perform one percent of the operation steps++; if (steps > pd->maximum()) t->stop(); } void Operation::cancel() { t->stop(); //... cleanup }
In both modes the progress dialog may be customized by replacing the child widgets with custom widgets by using setLabel(), setBar(), and setCancelButton(). The functions setLabelText() and setCancelButtonText() set the texts shown.

See also QDialog, QProgressBar, GUI Design Handbook: Progress Indicator, Find Files Example, and Pixelator Example.
Property Documentation
autoClose : bool
This property holds whether the dialog gets hidden by reset()
The default is true.
Access functions:
| bool | autoClose() const |
| void | setAutoClose(bool close) |
See also setAutoReset().
autoReset : bool
This property holds whether the progress dialog calls reset() as soon as value() equals maximum()
The default is true.
Access functions:
| bool | autoReset() const |
| void | setAutoReset(bool reset) |
See also setAutoClose().
labelText : QString
This property holds the label's text
The default text is an empty string.
Access functions:
| QString | labelText() const |
| void | setLabelText(const QString &text) |
maximum : int
This property holds the highest value represented by the progress bar
The default is 100.
Access functions:
| int | maximum() const |
| void | setMaximum(int maximum) |
See also minimum and setRange().
minimum : int
This property holds the lowest value represented by the progress bar
The default is 0.
Access functions:
| int | minimum() const |
| void | setMinimum(int minimum) |
See also maximum and setRange().
minimumDuration : int
This property holds the time that must pass before the dialog appears
If the expected duration of the task is less than the minimumDuration, the dialog will not appear at all. This prevents the dialog popping up for tasks that are quickly over. For tasks that are expected to exceed the minimumDuration, the dialog will pop up after the minimumDuration time or as soon as any progress is set.
If set to 0, the dialog is always shown as soon as any progress is set. The default is 4000 milliseconds.
Access functions:
| int | minimumDuration() const |
| void | setMinimumDuration(int ms) |
value : int
This property holds the current amount of progress made.
For the progress dialog to work as expected, you should initially set this property to QProgressDialog::minimum() and finally set it to QProgressDialog::maximum(); you can call setValue() any number of times in-between.
Warning: If the progress dialog is modal (see QProgressDialog::QProgressDialog()), setValue() calls QCoreApplication::processEvents(), so take care that this does not cause undesirable re-entrancy in your code. For example, don't use a QProgressDialog inside a paintEvent()!
Access functions:
| int | value() const |
| void | setValue(int progress) |
wasCanceled : const bool
This property holds whether the dialog was canceled
Access functions:
| bool | wasCanceled() const |