Source Code (Use browser search to find items of interest.)
Class Index
qt'QProgressData (./qt-2.1.0/src/dialogs/qprogressdialog.cpp:46)
struct QProgressData
{
QProgressData( QProgressDialog* that, QWidget* parent,
const QString& labelText,
int totalSteps ) :
creator( parent ),
label( new QLabel(labelText,that,"label") ),
cancel( 0 ),
bar( new QProgressBar(totalSteps,that,"bar") ),
shown_once( FALSE ),
cancellation_flag( FALSE ),
showTime( defaultShowTime )
{
label->setAlignment( that->style() != Qt::WindowsStyle
? Qt::AlignCenter
: Qt::AlignLeft|Qt::AlignVCenter );
}
QWidget *creator;
QLabel *label;
QPushButton *cancel;
QProgressBar *bar;
bool shown_once;
bool cancellation_flag;
QTime starttime;
QCursor parentCursor;
int showTime;
bool autoClose;
bool autoReset;
bool forceHide;
};
// REVISED: paul
/*!
\class QProgressDialog qprogressdialog.h
\brief Provides feedback on the progress of a slow operation.
\ingroup dialogs
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 it
freezes the application, making it inaccessible to the user for the
duration of the operation. Do the operation in a loop, calling
\l setProgress() at intervals, and checking for cancellation with
wasCancelled().
Example:
\code
QProgressDialog progress( "Copying files...", "Abort Copy", numFiles,
this, "progress", TRUE );
for (int i=0; i<numFiles; i++) {
progress.setProgress( i );
if ( progress.wasCancelled() )
break;
... // copy one file
}
progress.setProgress( numFiles );
\endcode
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 \l setProgress() at
intervals.
Example:
\code
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
}
\endcode
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.
<img src=qprogdlg-m.png> <img src=qprogdlg-w.png>
\sa QDialog QProgressBar
<a href="guibooks.html#fowler">GUI Design Handbook: Progress Indicator</a>
*/
/*!
Returns the QLabel currently being displayed above the progress bar.
Note that this QLabel remains owned by the QProgressDialog.
\sa setLabel()
*/