
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQ3ProgressDialog class provides feedback on the progress of a slow operation.More...
| Header: | #include <Q3ProgressDialog> |
| Inherits: | QDialog |
|
|
| Q3ProgressDialog(QWidget * creator, const char * name, bool modal = false, Qt::WindowFlags f = 0) | |
| Q3ProgressDialog(const QString & labelText, const QString & cancelButtonText, int totalSteps, QWidget * creator = 0, const char * name = 0, bool modal = false, Qt::WindowFlags f = 0) | |
| Q3ProgressDialog(QWidget * creator = 0, Qt::WindowFlags f = 0) | |
| Q3ProgressDialog(const QString & labelText, const QString & cancelButtonText, int totalSteps, QWidget * creator = 0, Qt::WindowFlags f = 0) | |
| ~Q3ProgressDialog() | |
| bool | autoClose() const |
| bool | autoReset() const |
| QString | labelText() const |
| int | minimumDuration() const |
| int | progress() const |
| void | setAutoClose(bool b) |
| void | setAutoReset(bool b) |
| void | setBar(Q3ProgressBar * bar) |
| void | setCancelButton(QPushButton * cancelButton) |
| void | setLabel(QLabel * label) |
| int | totalSteps() const |
| bool | wasCanceled() const |
| bool | wasCancelled() const |
| virtual QSize | sizeHint() const |
| void | cancel() |
| void | reset() |
| void | setCancelButtonText(const QString & cancelButtonText) |
| void | setLabelText(const QString &) |
| void | setMinimumDuration(int ms) |
| void | setProgress(int progress) |
| void | setProgress(int progress, int totalSteps) |
| void | setTotalSteps(int totalSteps) |
| virtual void | changeEvent(QEvent * ev) |
| virtual void | closeEvent(QCloseEvent * e) |
| virtual void | resizeEvent(QResizeEvent *) |
| virtual void | showEvent(QShowEvent * e) |
| void | forceShow() |
TheQ3ProgressDialog 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.Q3ProgressDialog 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 beyondminimumDuration() (4 seconds by default).
UsesetTotalSteps() (or the constructor) to set the number of "steps" in the operation and callsetProgress() 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 finished when you callsetProgress() withtotalSteps() as its argument.
The dialog automatically resets and hides itself at the end of the operation. UsesetAutoReset() andsetAutoClose() to change this behavior.
There are two ways of usingQ3ProgressDialog: modal and modeless.
Using a modalQ3ProgressDialog is simpler for the programmer, but you must callQApplication::processEvents() orQEventLoop::processEvents(ExcludeUserInput) to keep the event loop running to ensure that the application doesn't freeze. Do the operation in a loop, callsetProgress() at intervals, and check for cancellation withwasCanceled(). For example:
Q3ProgressDialogprogress("Copying files...","Abort Copy", numFiles,this,"progress",true);for (int i=0; i< numFiles; i++) { progress.setProgress(i); qApp->processEvents();if (progress.wasCanceled())break;//... copy one file}progress.setProgress(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 onQTimer (orQObject::timerEvent()),QSocketNotifier, orQUrlOperator; or performed in a separate thread. AQ3ProgressBar 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 thecanceled() signal to a slot that stops the operation, and callsetProgress() at intervals. For example:
Operation::Operation(QObject*parent=0) :QObject(parent), steps(0){ pd=newQ3ProgressDialog("Operation in progress.","Cancel",100); connect(pd, SIGNAL(canceled()),this, SLOT(cancel())); t=newQTimer(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 usingsetLabel(),setBar(), andsetCancelButton(). The functionssetLabelText() andsetCancelButtonText() set the texts shown.


See alsoQDialog,Q3ProgressBar, andGUI Design Handbook: Progress Indicator.
This property holds whether the dialog gets hidden by reset().
The default is true.
Access functions:
| bool | autoClose() const |
| void | setAutoClose(bool b) |
See alsosetAutoReset().
This property holds whether the progress dialog calls reset() as soon as progress() equals totalSteps().
The default is true.
Access functions:
| bool | autoReset() const |
| void | setAutoReset(bool b) |
See alsosetAutoClose().
This property holds the label's text.
The default text is an empty string.
Access functions:
| QString | labelText() const |
| void | setLabelText(const QString &) |
This property holds the time that must pass before the dialog appears.
If the expected duration of the task is less than the minimumDuration, the dialog will not appear at all. This prevents the dialog popping up for tasks that are quickly over. For tasks that are expected to exceed the minimumDuration, the dialog will pop up after the minimumDuration time or as soon as any progress is set.
If set to 0, the dialog is always shown as soon as any progress is set. The default is 4000 milliseconds.
Access functions:
| int | minimumDuration() const |
| void | setMinimumDuration(int ms) |
This property holds the current amount of progress made.
For the progress dialog to work as expected, you should initially set this property to 0 and finally set it toQ3ProgressDialog::totalSteps(); you can call setProgress() any number of times in-between.
Warning: If the progress dialog is modal (seeQ3ProgressDialog::Q3ProgressDialog()), this function callsQApplication::processEvents(), so take care that this does not cause undesirable re-entrancy in your code. For example, don't use aQ3ProgressDialog inside apaintEvent()!
Access functions:
| int | progress() const |
| void | setProgress(int progress) |
| void | setProgress(int progress, int totalSteps) |
See alsototalSteps.
This property holds the total number of steps.
The default is 0.
Access functions:
| int | totalSteps() const |
| void | setTotalSteps(int totalSteps) |
This property holds whether the dialog was canceled.
Access functions:
| bool | wasCanceled() const |
See alsosetProgress().
This property holds whether the dialog was canceled.
UsewasCanceled instead.
This property was introduced in Qt 4.2.
Access functions:
| bool | wasCancelled() const |
Constructs a progress dialog.
Default settings:
Thecreator argument is the widget to use as the dialog's parent. Thename,modal, and the widget flags,f, are passed to theQDialog::QDialog() constructor. Ifmodal is false (the default), you must have an event loop proceeding for any redrawing of the dialog to occur. Ifmodal is true, the dialog ensures that events are processed when needed.
See alsosetLabelText(),setLabel(),setCancelButtonText(),setCancelButton(), andsetTotalSteps().
Constructs a progress dialog.
ThelabelText is text used to remind the user what is progressing.
ThecancelButtonText is the text to display on the cancel button, or 0 if no cancel button is to be shown.
ThetotalSteps is the total 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 would be 50. Before examining the first file, callsetProgress(0). As each file is processed callsetProgress(1),setProgress(2), etc., finally callingsetProgress(50) after examining the last file.
Thecreator argument is the widget to use as the dialog's parent. Thename,modal, and widget flags,f, are passed to theQDialog::QDialog() constructor. Ifmodal is false (the default), you will must have an event loop proceeding for any redrawing of the dialog to occur. Ifmodal is true, the dialog ensures that events are processed when needed.
See alsosetLabelText(),setLabel(),setCancelButtonText(),setCancelButton(), andsetTotalSteps().
Constructs a progress dialog.
Default settings:
Thecreator argument is the widget to use as the dialog's parent. The widget flags,f, are passed to theQDialog::QDialog() constructor.
See alsosetLabelText(),setLabel(),setCancelButtonText(),setCancelButton(), andsetTotalSteps().
Constructs a progress dialog.
ThelabelText is text used to remind the user what is progressing.
ThecancelButtonText is the text to display on the cancel button, or 0 if no cancel button is to be shown.
ThetotalSteps is the total 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 would be 50. Before examining the first file, callsetProgress(0). As each file is processed callsetProgress(1),setProgress(2), etc., finally callingsetProgress(50) after examining the last file.
Thecreator argument is the widget to use as the dialog's parent. The widget flags,f, are passed to theQDialog::QDialog() constructor.
See alsosetLabelText(),setLabel(),setCancelButtonText(),setCancelButton(), andsetTotalSteps().
Destroys the progress dialog.
[slot]void Q3ProgressDialog::cancel()Resets the progress dialog.wasCanceled() becomes true until the progress dialog is reset. The progress dialog becomes hidden.
[signal]void Q3ProgressDialog::canceled()This signal is emitted when the cancel button is clicked. It is connected to thecancel() slot by default.
See alsowasCanceled().
[signal]void Q3ProgressDialog::cancelled()Usecanceled() instead.
[virtual protected]void Q3ProgressDialog::changeEvent(QEvent * ev)Reimplemented fromQWidget::changeEvent().
[virtual protected]void Q3ProgressDialog::closeEvent(QCloseEvent * e)Reimplemented fromQWidget::closeEvent().
[protected slot]void Q3ProgressDialog::forceShow()Shows the dialog if it is still hidden after the algorithm has been started andminimumDuration milliseconds have passed.
See alsosetMinimumDuration().
[slot]void Q3ProgressDialog::reset()Resets the progress dialog. The progress dialog becomes hidden ifautoClose() is true.
See alsosetAutoClose() andsetAutoReset().
[virtual protected]void Q3ProgressDialog::resizeEvent(QResizeEvent *)Reimplemented fromQWidget::resizeEvent().
Sets the progress bar widget tobar. The progress dialog resizes to fit. The progress dialog takes ownership of the progressbar which will be deleted when necessary, so do not use a progress bar allocated on the stack.
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 alsosetCancelButtonText().
[slot]void Q3ProgressDialog::setCancelButtonText(constQString & cancelButtonText)Sets the cancel button's text tocancelButtonText.
See alsosetCancelButton().
Sets the label tolabel. 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 alsosetLabelText().
[virtual protected]void Q3ProgressDialog::showEvent(QShowEvent * e)Reimplemented fromQWidget::showEvent().
[virtual]QSize Q3ProgressDialog::sizeHint() constReimplemented fromQWidget::sizeHint().
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.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.