Classes - Annotated - Tree - Functions - Home - Structure

QProgressDialog Class Reference

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

#include <qprogressdialog.h>

Inherits QDialog.

List of all member functions.

Public Members

Public Slots

Signals

Properties

Protected Slots


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 to perform, and to indicate that the application has not frozen. It can also gives 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 computer 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 setTotalSteps() (or the constructor) to set the number of "steps" in the operation and call setProgress() as the operation progresses. The step value 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 0, and the progress dialog shows that the operation has completeed when you call setProgress() with totalSteps() as argument.

The dialog automatically resets and hides itslef at the end of the operation. Use setAutoReset() and setAutoClose() to change this behavior.

There are two ways of using QProgressDialog: modal and non-modal.

Using a modal QProgressDialog is simpler for the programmer, but you have to call qApp->processEvents() to keep the event loop running and prevent the application from freezing. Do the operation in a loop - call setProgress() at intervals and check for cancellation with wasCancelled(). For example:

    QProgressDialog progress( "Copying files...", "Abort Copy", numFiles,
                                this, "progress", TRUE );
    for (int i=0; i<numFiles; i++) {
        progress.setProgress( i );
        qApp->processEvents();

        if ( progress.wasCancelled() )
            break;
        ... // copy one file
    }
    progress.setProgress( numFiles );
  

A non-modal 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 non-modal progress dialog.

You need an event loop to be running. Connect the cancelled() signal to a slot that stops the operation, and call setProgress() at intervals.

Example:

  Operation::Operation( QObject *parent = 0 )
      : QObject( parent ), steps(0)
  {
      pd = new QProgressDialog( "Operation in progress.", "Cancel", 100 );
      connect( pd, SIGNAL(cancelled()), this, SLOT(cancel()) );
      t = new QTimer( this );
      connect( t, SIGNAL(timeout()), this, SLOT(perform()) );
      t->start(0);
  }

  void Operation::perform()
  {
      pd->setProgress( steps );
      ...       //perform one percent of the operation
      steps++;
      if ( steps > pd->totalSteps() )
                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 and GUI Design Handbook: Progress Indicator.


Member Function Documentation

QProgressDialog::QProgressDialog ( QWidget * creator = 0, const char * name = 0, bool modal = FALSE, WFlags f = 0 )

Constructs a progress dialog.

Default settings:

The top level parent of creator becomes the parent of the dialog. name, modal, and f are sent to the QDialog::QDialog() constructor. Note that if modal is FALSE (the default), you will need to have an event loop proceeding for any redrawing of the dialog to occur. If it is TRUE, the dialog ensures that events are processed when needed.

See also labelText, setLabel(), setCancelButtonText(), setCancelButton() and totalSteps.

QProgressDialog::QProgressDialog ( const QString & labelText, const QString & cancelButtonText, int totalSteps, QWidget * creator = 0, const char * name = 0, bool modal = FALSE, WFlags f = 0 )

Constructs a progress dialog.

labelText is text telling the user what is progressing.

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

totalSteps is the total number of steps in the operation of which this progress dialog shows the progress. For example, if the operation is to examine 50 files, this value would be 50. Before examining the first file, call setProgress(0); call setProgress(50) after examining the last file .

name, modal, and f are sent to the QDialog::QDialog() constructor. Note that if modal is FALSE (the default), you will need to have an event loop proceeding for any redrawing of the dialog to occur. If it is TRUE, the dialog ensures that events are processed when needed.

The creator argument is the widget to use as the dialog's parent. If creator is not a top level widget the argument passed on to the QDialog constructor will be 0.

See also labelText, setLabel(), setCancelButtonText(), setCancelButton() and totalSteps.

QProgressDialog::~QProgressDialog ()

Destroys the progress dialog.

bool QProgressDialog::autoClose () const

Returns TRUE if the dialog gets hidden by reset(); otherwise returns FALSE. See the "autoClose" property for details.

bool QProgressDialog::autoReset () const

Returns TRUE if the progress dialog calls reset() as soon as progress() equals totalSteps(); otherwise returns FALSE. See the "autoReset" property for details.

void QProgressDialog::cancel () [slot]

Resets the progress dialog. wasCancelled() becomes TRUE until the progress dialog is reset. The progress dialog becomes hidden.

void QProgressDialog::cancelled () [signal]

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

See also wasCancelled.

Example: progress/progress.cpp.

void QProgressDialog::forceShow () [protected slot]

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

See also minimumDuration.

QString QProgressDialog::labelText () const

Returns the labels text. See the "labelText" property for details.

int QProgressDialog::minimumDuration () const

Returns the duration the progress has to take before the dialog opens. See the "minimumDuration" property for details.

int QProgressDialog::progress () const

Returns the current amount of progress made. See the "progress" property for details.

void QProgressDialog::reset () [slot]

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

See also autoClose and autoReset.

void QProgressDialog::setAutoClose ( bool b )

Sets whether the dialog gets hidden by reset() to b. See the "autoClose" property for details.

void QProgressDialog::setAutoReset ( bool b )

Sets whether the progress dialog calls reset() as soon as progress() equals totalSteps() to b. See the "autoReset" property for details.

void QProgressDialog::setBar ( QProgressBar * bar )

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.

void QProgressDialog::setCancelButton ( QPushButton * cancelButton )

Sets the cancel button to 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().

void QProgressDialog::setCancelButtonText ( const QString & cancelButtonText ) [slot]

Sets the cancel button's text to cancelButtonText.

See also setCancelButton().

void QProgressDialog::setLabel ( QLabel * label )

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 labelText.

Example: progress/progress.cpp.

void QProgressDialog::setLabelText ( const QString & ) [slot]

Sets the labels text. See the "labelText" property for details.

void QProgressDialog::setMinimumDuration ( int ms ) [slot]

Sets the duration the progress has to take before the dialog opens to ms. See the "minimumDuration" property for details.

void QProgressDialog::setProgress ( int progress ) [slot]

Sets the current amount of progress made to progress. See the "progress" property for details.

void QProgressDialog::setTotalSteps ( int totalSteps ) [slot]

Sets the total number of steps to totalSteps. See the "totalSteps" property for details.

QSize QProgressDialog::sizeHint () const [virtual]

Returns a size that fits the contents of the progress dialog. The progress dialog resizes itself as required, so this should not be needed in user code.

Reimplemented from QDialog.

int QProgressDialog::totalSteps () const

Returns the total number of steps. See the "totalSteps" property for details.

bool QProgressDialog::wasCancelled () const

Returns TRUE if the dialog was cancelled; otherwise returns FALSE. See the "wasCancelled" property for details.


Property Documentation

bool autoClose

This property holds whether the dialog gets hidden by reset().

Set this property's value with setAutoClose() and get this property's value with autoClose().

See also autoReset.

bool autoReset

This property holds whether the progress dialog calls reset() as soon as progress() equals totalSteps().

Set this property's value with setAutoReset() and get this property's value with autoReset().

See also autoClose.

QString labelText

This property holds the labels text.

Set this property's value with setLabelText() and get this property's value with labelText().

int minimumDuration

This property holds the duration the progress has to take before the dialog opens.

The dialog will not appear if the anticipated duration of the progressing task is less than the minimum duration.

If set to 0, the dialog is always shown as soon as any progress is set.

Set this property's value with setMinimumDuration() and get this property's value with minimumDuration().

int progress

This property holds the current amount of progress made.

For the progress dialog to work correctly, you must initially set this property to 0 and finally to QProgressDialog::totalSteps(); you may call it any number of times in-between.

Warning: If the progress dialog is modal (see QProgressDialog::QProgressDialog()), this function calls QApplication::processEvents(), so take care that this does not cause undesirable re-entrancy to your code. For example, don't use a QProgressDialog inside a paintEvent()!

See also totalSteps.

Set this property's value with setProgress() and get this property's value with progress().

int totalSteps

This property holds the total number of steps.

Set this property's value with setTotalSteps() and get this property's value with totalSteps().

bool wasCancelled

This property holds whether the dialog was cancelled.

Get this property's value with wasCancelled().

See also progress.


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta2