Qtopia Home - Classes - Hierachy - Annotated - Functions - Licenses - Reference |
|
Provides feedback on the progress of a slow operation. More...
#include <qprogressdialog.h>
Inherits QSemiModal.
A progress dialog is used to give the user an indication of how long an operation is going to take to perform, and to reassure them that the application has not frozen. It also gives the user an opportunity to abort the operation.
A potential problem with progress dialogs is that it is difficult to know when to use them, as 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 eg. the number of files copied, the number of bytes received, or the number of iterations through the main loop of your algorithm. You must call setProgress() with parameter 0 initially, and with parameter totalSteps() at the end.
The dialog will automatically be reset and hidden 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, calling setProgress() at intervals, and checking for cancellation with wasCancelled().
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, using setLabel(), setBar() and setCancelButton(). The functions setLabelText() and setCancelButtonText() sets the texts shown.
See also QDialog, QProgressBar, and GUI Design Handbook: Progress Indicator.
Default settings:
parent, name, modal, and f are sent to the QSemiModal::QSemiModal() 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 events are processed when needed.
See also setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), and setTotalSteps().
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, then before examining the first file, call setProgress(0), and after examining the last file call setProgress(50).
name, modal, and f are sent to the QSemiModal::QSemiModal() 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 events are processed when needed.
See also setLabelText(), setLabel(), setCancelButtonText(), setCancelButton(), and setTotalSteps().
See also setAutoClose().
See also setAutoReset().
This signal is emitted when the cancel button is clicked. It is connected to the cancel() slot by default.
See also wasCancelled().
See also setMinimumDuration().
See also setLabelText.
See also setMinimumDuration().
See also setProgress().
See also setAutoClose() and setAutoReset().
See also autoClose() and setAutoReset().
See also autoReset() and setAutoClose().
See also setCancelButtonText().
See also setCancelButton().
See also setLabelText().
See also setLabel().
If ms is 0 the dialog is always shown as soon as any progress is set.
See also minimumDuration().
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 progress().
See also totalSteps() and QProgressBar::setTotalSteps().
Reimplemented from QWidget.
See also setTotalSteps() and QProgressBar::totalSteps().
See also setProgress(), cancel(), and cancelled().
This file is part of the Qtopia platform, copyright © 1995-2005 Trolltech, all rights reserved.
| Copyright © 2005 Trolltech | Trademarks | Qtopia version 2.2.0
|