QProgressDialog Class
The QProgressDialog class shows the progress of a long task and lets the user cancel it. More…
| Header | #include <QProgressDialog> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QDialog |
List of all members, including inherited members
Public Functions
| QProgressDialog(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()) | |
| QProgressDialog(const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()) | |
| ~QProgressDialog() | |
| bool | wasCanceled() const |
| int | minimum() const |
| int | maximum() const |
| int | value() const |
| QString | labelText() const |
| int | minimumDuration() const |
| void | setAutoReset(bool reset) |
| bool | autoReset() const |
| void | setAutoClose(bool close) |
| bool | autoClose() const |
Reimplemented Public Functions
Public Slots
| void | cancel() |
| void | reset() |
| void | setMaximum(int maximum) |
| void | setMinimum(int minimum) |
| void | setRange(int minimum, int maximum) |
| void | setValue(int progress) |
| void | setLabelText(const QString &text) |
| void | setCancelButtonText(const QString &text) |
| void | setMinimumDuration(int ms) |
Signals
| void | canceled() |
Reimplemented Protected Functions
| void | closeEvent(QCloseEvent *event) |
| void | keyPressEvent(QKeyEvent *event) |
Detailed Description
A progress dialog has a label, a progress bar and a Cancel button. It shows itself only if the task takes longer than minimumDuration(), four seconds by default, so that short tasks do not flash a dialog. Call setValue() as the task goes on, and check wasCanceled():
QProgressDialog progress("Copying files...", "Abort", 0, files.size(), window);
progress.setWindowModality(Qt::WindowModal);
for (int i = 0; i < files.size(); ++i) {
progress.setValue(i);
if (progress.wasCanceled())
break;
copy(files[i]);
}
progress.setValue(files.size());
A window-modal progress dialog processes the events of the application in setValue(), so that it is painted and its Cancel button works while the task runs in the same thread.
When the value reaches the maximum the dialog resets and hides, unless autoReset() or autoClose() are turned off.
See also QProgressBar and QDialog.
Differences from Qt
There is no setBar(), setLabel() or setCancelButton() to replace the parts, and no open() with a receiver. The texts are not translated.
Member Function Documentation
QProgressDialog::QProgressDialog(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
Constructs a progress dialog with the given parent and window flags, a Cancel button and the range 0 to 100.
QProgressDialog::QProgressDialog(const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())
Constructs a progress dialog with the given labelText, cancelButtonText, range minimum to maximum, parent and window flags. An empty cancelButtonText gives a dialog without a Cancel button.
QProgressDialog::~QProgressDialog()
Destroys the progress dialog.
bool QProgressDialog::autoClose() const
Returns whether the dialog hides when it resets. The default is true.
bool QProgressDialog::autoReset() const
Returns whether the dialog resets when the value reaches the maximum. The default is true.
void QProgressDialog::cancel()
Cancels the dialog: it hides and resets, and wasCanceled() is true until it is reset again.
See also canceled() and reset().
void QProgressDialog::canceled()
This signal is emitted when the user clicks Cancel, presses Escape or closes the window. It is connected to cancel().
void QProgressDialog::closeEvent(QCloseEvent *event)
Reimplements QDialog::closeEvent().
Closing the window cancels the dialog.
void QProgressDialog::keyPressEvent(QKeyEvent *event)
Reimplements QDialog::keyPressEvent().
Escape cancels the dialog.
QString QProgressDialog::labelText() const
Returns the text of the label.
See also setLabelText().
int QProgressDialog::maximum() const
Returns the largest value, 100 by default.
See also setMaximum().
int QProgressDialog::minimum() const
Returns the smallest value, 0 by default.
See also setMinimum().
int QProgressDialog::minimumDuration() const
Returns how long, in milliseconds, the task must take before the dialog is shown. The default is 4000.
See also setMinimumDuration().
void QProgressDialog::reset()
Resets the dialog: the progress bar is emptied, wasCanceled() is false again, and the dialog hides if autoClose() is true.
See also autoClose() and autoReset().
void QProgressDialog::setAutoClose(bool close)
Sets whether the dialog hides when it resets to close.
See also autoClose().
void QProgressDialog::setAutoReset(bool reset)
Sets whether the dialog resets when the value reaches the maximum to reset.
See also autoReset().
void QProgressDialog::setCancelButtonText(const QString &text)
Sets the text of the Cancel button to text. An empty text hides the button.
void QProgressDialog::setLabelText(const QString &text)
Sets the text of the label to text.
See also labelText().
void QProgressDialog::setMaximum(int maximum)
Sets the largest value to maximum.
See also maximum() and setRange().
void QProgressDialog::setMinimum(int minimum)
Sets the smallest value to minimum.
See also minimum() and setRange().
void QProgressDialog::setMinimumDuration(int ms)
Sets how long, in milliseconds, the task must take before the dialog is shown to ms.
See also minimumDuration().
void QProgressDialog::setRange(int minimum, int maximum)
Sets the range to minimum to maximum.
void QProgressDialog::setValue(int progress)
Sets the progress to progress. The first value at the minimum starts the clock; later values show the dialog once the task looks like it will take longer than minimumDuration(). At the maximum the dialog resets if autoReset() is true. A window-modal dialog processes the pending events.
See also value() and minimumDuration().
QSize QProgressDialog::sizeHint() const
Reimplements QDialog::sizeHint().
int QProgressDialog::value() const
Returns the progress, or minimum() - 1 if the dialog was reset.
See also setValue().
bool QProgressDialog::wasCanceled() const
Returns whether the dialog was canceled, by the user or by cancel().