|  | Home · All Classes · Modules

# QProgressDialog Class Reference
[QtGui module]

The QProgressDialog class provides feedback on the progress of a slow operation. More...

Inherits QDialog.

### Methods

- __init__ (self, QWidget parent = None, Qt.WindowFlags f = 0)
- __init__ (self, QString labelText, QString cancelButtonText, int minimum, int maximum, QWidget parent = None, Qt.WindowFlags f = 0)
- bool autoClose (self)
- bool autoReset (self)
- cancel (self)
- changeEvent (self, QEvent)
- closeEvent (self, QCloseEvent)
- forceShow (self)
- QString labelText (self)
- int maximum (self)
- int minimum (self)
- int minimumDuration (self)
- reset (self)
- resizeEvent (self, QResizeEvent)
- setAutoClose (self, bool b)
- setAutoReset (self, bool b)
- setBar (self, QProgressBar bar)
- setCancelButton (self, QPushButton button)
- setCancelButtonText (self, QString)
- setLabel (self, QLabel label)
- setLabelText (self, QString)
- setMaximum (self, int maximum)
- setMinimum (self, int minimum)
- setMinimumDuration (self, int ms)
- setRange (self, int minimum, int maximum)
- setValue (self, int progress)
- showEvent (self, QShowEvent e)
- QSize sizeHint (self)
- int value (self)
- bool wasCanceled (self)

### Qt Signals

- void canceled ()

## Detailed Description

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.

Using a modal QProgressDialog is simpler for the programmer, but you must call QApplication.processEvents() or QEventLoop.processEvents(ExcludeUserInput) to keep the event loop running to ensure that the application doesn't freeze. 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);
             qApp->processEvents();

             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.

## Method Documentation

### QProgressDialog.__init__ (self, QWidget parent = None, Qt.WindowFlags f = 0)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a progress dialog.

Default settings:

- The label text is empty.
- The cancel button text is (translated) "Cancel".
- minimum is 0;
- maximum is 100

The parent argument is dialog's parent widget. The widget flags, f, are passed to the QDialog.QDialog() constructor.

See also setLabelText(), setCancelButtonText(), setCancelButton(), setMinimum(), and setMaximum().

### QProgressDialog.__init__ (self, QString labelText, QString cancelButtonText, int minimum, int maximum, QWidget parent = None, Qt.WindowFlags f = 0)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a progress dialog.

The labelText is the text used to remind the user what is progressing.

The cancelButtonText is the text to display on the cancel button, or 0 if no cancel button is to be shown.

The minimum and maximum is the number of steps in the operation for which this progress dialog shows progress. For example, if the operation is to examine 50 files, this value minimum value would be 0, and the maximum would be 50. Before examining the first file, call setValue(0). As each file is processed call setValue(1), setValue(2), etc., finally calling setValue(50) after examining the last file.

The parent argument is the dialog's parent widget. The and widget flags, f, are passed to the QDialog.QDialog() constructor.

See also setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), setMinimum(), and setMaximum().

### bool QProgressDialog.autoClose (self)

### bool QProgressDialog.autoReset (self)

### QProgressDialog.cancel (self)

This method is also a Qt slot with the C++ signature `void cancel()`.

Resets the progress dialog. wasCanceled() becomes true until the progress dialog is reset. The progress dialog becomes hidden.

### QProgressDialog.changeEvent (self, QEvent)

### QProgressDialog.closeEvent (self, QCloseEvent)

### QProgressDialog.forceShow (self)

Shows the dialog if it is still hidden after the algorithm has been started and minimumDuration milliseconds have passed.

See also setMinimumDuration().

### QString QProgressDialog.labelText (self)

### int QProgressDialog.maximum (self)

### int QProgressDialog.minimum (self)

### int QProgressDialog.minimumDuration (self)

### QProgressDialog.reset (self)

This method is also a Qt slot with the C++ signature `void reset()`.

Resets the progress dialog. The progress dialog becomes hidden if autoClose() is true.

See also setAutoClose() and setAutoReset().

### QProgressDialog.resizeEvent (self, QResizeEvent)

### QProgressDialog.setAutoClose (self, bool b)

### QProgressDialog.setAutoReset (self, bool b)

### QProgressDialog.setBar (self, QProgressBar bar)

The bar argument has it's ownership transferred to Qt.

Sets the progress bar widget to bar. The progress dialog resizes to fit. The progress dialog takes ownership of the progress bar which will be deleted when necessary, so do not use a progress bar allocated on the stack.

### QProgressDialog.setCancelButton (self, QPushButton button)

The button argument has it's ownership transferred to Qt.

Sets the cancel button to the push button, cancelButton. The progress dialog takes ownership of this button which will be deleted when necessary, so do not pass the address of an object that is on the stack, i.e. use new() to create the button.

See also setCancelButtonText().

### QProgressDialog.setCancelButtonText (self, QString)

This method is also a Qt slot with the C++ signature `void setCancelButtonText(const QString&)`.

Sets the cancel button's text to cancelButtonText.

See also setCancelButton().

### QProgressDialog.setLabel (self, QLabel label)

The label argument has it's ownership transferred to Qt.

Sets the label to label. The progress dialog resizes to fit. The label becomes owned by the progress dialog and will be deleted when necessary, so do not pass the address of an object on the stack.

See also setLabelText().

### QProgressDialog.setLabelText (self, QString)

This method is also a Qt slot with the C++ signature `void setLabelText(const QString&)`.

### QProgressDialog.setMaximum (self, int maximum)

This method is also a Qt slot with the C++ signature `void setMaximum(int)`.

### QProgressDialog.setMinimum (self, int minimum)

This method is also a Qt slot with the C++ signature `void setMinimum(int)`.

### QProgressDialog.setMinimumDuration (self, int ms)

This method is also a Qt slot with the C++ signature `void setMinimumDuration(int)`.

### QProgressDialog.setRange (self, int minimum, int maximum)

Sets the progress dialog's minimum and maximum values to minimum and maximum, respectively.

If maximum is smaller than minimum, minimum becomes the only legal value.

If the current value falls outside the new range, the progress dialog is reset with reset().

See also minimum and maximum.

### QProgressDialog.setValue (self, int progress)

This method is also a Qt slot with the C++ signature `void setValue(int)`.

### QProgressDialog.showEvent (self, QShowEvent e)

### QSize QProgressDialog.sizeHint (self)

Returns a size that fits the contents of the progress dialog. The progress dialog resizes itself as required, so you should not need to call this yourself.

Reimplemented from QWidget.

### int QProgressDialog.value (self)

### bool QProgressDialog.wasCanceled (self)

## Qt Signal Documentation

### void canceled ()

This signal is emitted when the cancel button is clicked. It is connected to the cancel() slot by default.

See also wasCanceled().