![]() |
Home · Overviews · Examples |
The QProgressDialog class provides feedback on the progress of a slow operation. More...
Inherits QDialog.
The QProgressDialog class provides feedback on the progress of a slow operation.
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.
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()), QSocketNotifier, or QUrlOperator; 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, SIGNAL(canceled()), this, SLOT(cancel())); t = new QTimer(this); connect(t, SIGNAL(timeout()), this, SLOT(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.
The Standard Dialogs example shows how to use QProgressDialog as well as other built-in Qt dialogs.
See also QDialog, QProgressBar, GUI Design Handbook: Progress Indicator, Find Files Example, and Pixelator Example.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.3.5_01 |