#include <qprogressdialog.h>
Inheritance diagram for QProgressDialog:


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:
snippets/dialogs/dialogs.cpp QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this); 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 } } }
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 {dialogs/standarddialogs}{Standard Dialogs} example shows how to use QProgressDialog as well as other built-in Qt dialogs.
A progress dialog shown in the Plastique widget style.
Definition at line 41 of file qprogressdialog.h.
Public Slots | |
| void | cancel () |
| void | reset () |
| void | setMaximum (int maximum) |
| void | setMinimum (int minimum) |
| void | setValue (int progress) |
| void | setLabelText (const QString &) |
| void | setCancelButtonText (const QString &) |
| void | setMinimumDuration (int ms) |
Signals | |
| void | canceled () |
Public Member Functions | |
| QProgressDialog (QWidget *parent=0, Qt::WindowFlags f=0) | |
| QProgressDialog (const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *parent=0, Qt::WindowFlags f=0) | |
| ~QProgressDialog () | |
| void | setLabel (QLabel *label) |
| void | setCancelButton (QPushButton *button) |
| void | setBar (QProgressBar *bar) |
| bool | wasCanceled () const |
| int | minimum () const |
| int | maximum () const |
| void | setRange (int minimum, int maximum) |
| int | value () const |
| QSize | sizeHint () const |
| QString | labelText () const |
| int | minimumDuration () const |
| void | setAutoReset (bool b) |
| bool | autoReset () const |
| void | setAutoClose (bool b) |
| bool | autoClose () const |
Protected Slots | |
| void | forceShow () |
Protected Member Functions | |
| void | resizeEvent (QResizeEvent *) |
| void | closeEvent (QCloseEvent *) |
| void | changeEvent (QEvent *) |
| void | showEvent (QShowEvent *e) |
| QProgressDialog::QProgressDialog | ( | QWidget * | parent = 0, |
|
| Qt::WindowFlags | f = 0 | |||
| ) | [explicit] |
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.
Definition at line 235 of file qprogressdialog.cpp.
References d, and QString::fromLatin1().
00236 : QDialog(*(new QProgressDialogPrivate), parent, f) 00237 { 00238 Q_D(QProgressDialog); 00239 d->init(QString::fromLatin1(""), tr("Cancel"), 0, 100); 00240 }
Here is the call graph for this function:

| QProgressDialog::QProgressDialog | ( | const QString & | labelText, | |
| const QString & | cancelButtonText, | |||
| int | minimum, | |||
| int | maximum, | |||
| QWidget * | parent = 0, |
|||
| Qt::WindowFlags | f = 0 | |||
| ) |
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.
Definition at line 264 of file qprogressdialog.cpp.
References d, and labelText().
00268 : QDialog(*(new QProgressDialogPrivate), parent, f) 00269 { 00270 Q_D(QProgressDialog); 00271 d->init(labelText, cancelButtonText, minimum, maximum); 00272 }
Here is the call graph for this function:

| QProgressDialog::~QProgressDialog | ( | ) |
| 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.
Definition at line 301 of file qprogressdialog.cpp.
References d, h, QWidget::height(), QWidget::hide(), QWidget::isVisible(), QWidget::parentWidget(), qMax(), QWidget::resize(), QWidget::setParent(), QWidget::show(), sizeHint(), w, and QWidget::width().
00302 { 00303 Q_D(QProgressDialog); 00304 delete d->label; 00305 d->label = label; 00306 if (label) { 00307 if (label->parentWidget() == this) { 00308 label->hide(); // until we resize 00309 } else { 00310 label->setParent(this, 0); 00311 } 00312 } 00313 int w = qMax(isVisible() ? width() : 0, sizeHint().width()); 00314 int h = qMax(isVisible() ? height() : 0, sizeHint().height()); 00315 resize(w, h); 00316 if (label) 00317 label->show(); 00318 }
Here is the call graph for this function:

| void QProgressDialog::setCancelButton | ( | QPushButton * | cancelButton | ) |
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.
Definition at line 357 of file qprogressdialog.cpp.
References canceled(), QObject::connect(), d, h, QWidget::height(), QWidget::hide(), QWidget::isVisible(), Qt::Key_Escape, QWidget::parentWidget(), qMax(), QWidget::resize(), QWidget::setParent(), QWidget::show(), SIGNAL, sizeHint(), w, and QWidget::width().
Referenced by setCancelButtonText().
00358 { 00359 Q_D(QProgressDialog); 00360 delete d->cancel; 00361 d->cancel = cancelButton; 00362 if (cancelButton) { 00363 if (cancelButton->parentWidget() == this) { 00364 cancelButton->hide(); // until we resize 00365 } else { 00366 cancelButton->setParent(this, 0); 00367 } 00368 connect(d->cancel, SIGNAL(clicked()), this, SIGNAL(canceled())); 00369 #ifndef QT_NO_SHORTCUT 00370 new QShortcut(Qt::Key_Escape, this, SIGNAL(canceled())); 00371 #endif 00372 } 00373 int w = qMax(isVisible() ? width() : 0, sizeHint().width()); 00374 int h = qMax(isVisible() ? height() : 0, sizeHint().height()); 00375 resize(w, h); 00376 if (cancelButton) 00377 cancelButton->show(); 00378 }
Here is the call graph for this function:

| 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, so do not use a progress bar allocated on the stack.
Definition at line 409 of file qprogressdialog.cpp.
References d, h, QWidget::height(), QWidget::isVisible(), qMax(), qWarning(), QWidget::resize(), sizeHint(), value(), w, and QWidget::width().
00410 { 00411 Q_D(QProgressDialog); 00412 #ifndef QT_NO_DEBUG 00413 if (value() > 0) 00414 qWarning("QProgressDialog::setBar: Cannot set a new progress bar " 00415 "while the old one is active"); 00416 #endif 00417 delete d->bar; 00418 d->bar = bar; 00419 int w = qMax(isVisible() ? width() : 0, sizeHint().width()); 00420 int h = qMax(isVisible() ? height() : 0, sizeHint().height()); 00421 resize(w, h); 00422 }
Here is the call graph for this function:

| bool QProgressDialog::wasCanceled | ( | ) | const |
Definition at line 430 of file qprogressdialog.cpp.
References d.
Referenced by Window::findFiles(), MainWindow::printImage(), and BatchTranslationDialog::startTranslation().
00431 { 00432 Q_D(const QProgressDialog); 00433 return d->cancellation_flag; 00434 }
| int QProgressDialog::minimum | ( | ) | const |
Definition at line 467 of file qprogressdialog.cpp.
References d.
Referenced by setValue().
00468 { 00469 Q_D(const QProgressDialog); 00470 return d->bar->minimum(); 00471 }
| int QProgressDialog::maximum | ( | ) | const |
Definition at line 446 of file qprogressdialog.cpp.
References d.
Referenced by setValue().
00447 { 00448 Q_D(const QProgressDialog); 00449 return d->bar->maximum(); 00450 }
| void QProgressDialog::setRange | ( | 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().
Definition at line 491 of file qprogressdialog.cpp.
References d.
Referenced by Window::findFiles().
00492 { 00493 Q_D(QProgressDialog); 00494 d->bar->setRange(minimum, maximum); 00495 }
| int QProgressDialog::value | ( | ) | const |
Definition at line 538 of file qprogressdialog.cpp.
References d.
Referenced by reset(), and setBar().
00539 { 00540 Q_D(const QProgressDialog); 00541 return d->bar->value(); 00542 }
| QSize QProgressDialog::sizeHint | ( | ) | const [virtual] |
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 QDialog.
Definition at line 620 of file qprogressdialog.cpp.
References d, h, QSize::height(), QStyle::pixelMetric(), QStyle::PM_DefaultLayoutSpacing, QStyle::PM_DefaultTopLevelMargin, qMax(), spacing, QWidget::style(), and QSize::width().
Referenced by setBar(), setCancelButton(), setCancelButtonText(), setLabel(), setLabelText(), setValue(), and showEvent().
00621 { 00622 Q_D(const QProgressDialog); 00623 QSize sh = d->label->sizeHint(); 00624 QSize bh = d->bar->sizeHint(); 00625 int margin = style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin); 00626 int spacing = style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing); 00627 int h = margin * 2 + bh.height() + sh.height() + spacing; 00628 if (d->cancel) 00629 h += d->cancel->sizeHint().height() + spacing; 00630 return QSize(qMax(200, sh.width() + 2 * margin), h); 00631 }
Here is the call graph for this function:

| QString QProgressDialog::labelText | ( | ) | const |
Definition at line 328 of file qprogressdialog.cpp.
References d.
Referenced by QProgressDialog().
00329 { 00330 Q_D(const QProgressDialog); 00331 if (d->label) 00332 return d->label->text(); 00333 return QString(); 00334 }
| int QProgressDialog::minimumDuration | ( | ) | const |
Definition at line 676 of file qprogressdialog.cpp.
References d.
00677 { 00678 Q_D(const QProgressDialog); 00679 return d->showTime; 00680 }
| void QProgressDialog::setAutoReset | ( | bool | b | ) |
Definition at line 702 of file qprogressdialog.cpp.
References d.
00703 { 00704 Q_D(QProgressDialog); 00705 d->autoReset = b; 00706 }
| bool QProgressDialog::autoReset | ( | ) | const |
Definition at line 708 of file qprogressdialog.cpp.
References d.
00709 { 00710 Q_D(const QProgressDialog); 00711 return d->autoReset; 00712 }
| void QProgressDialog::setAutoClose | ( | bool | b | ) |
Definition at line 723 of file qprogressdialog.cpp.
References d.
00724 { 00725 Q_D(QProgressDialog); 00726 d->autoClose = b; 00727 }
| bool QProgressDialog::autoClose | ( | ) | const |
Definition at line 729 of file qprogressdialog.cpp.
References d.
00730 { 00731 Q_D(const QProgressDialog); 00732 return d->autoClose; 00733 }
| void QProgressDialog::cancel | ( | ) | [slot] |
Resets the progress dialog. wasCanceled() becomes true until the progress dialog is reset. The progress dialog becomes hidden.
Definition at line 528 of file qprogressdialog.cpp.
00529 { 00530 Q_D(QProgressDialog); 00531 d->forceHide = true; 00532 reset(); 00533 d->forceHide = false; 00534 d->cancellation_flag = true; 00535 }
| void QProgressDialog::reset | ( | ) | [slot] |
Resets the progress dialog. The progress dialog becomes hidden if autoClose() is true.
Definition at line 505 of file qprogressdialog.cpp.
References d, QWidget::hide(), QWidget::parentWidget(), QWidget::setCursor(), and value().
Referenced by cancel(), and setValue().
00506 { 00507 Q_D(QProgressDialog); 00508 #ifndef QT_NO_CURSOR 00509 if (value() >= 0) { 00510 if (parentWidget()) 00511 parentWidget()->setCursor(d->parentCursor); 00512 } 00513 #endif 00514 if (d->autoClose || d->forceHide) 00515 hide(); 00516 d->bar->reset(); 00517 d->cancellation_flag = false; 00518 d->shown_once = false; 00519 d->forceTimer->stop(); 00520 }
| void QProgressDialog::setMaximum | ( | int | maximum | ) | [slot] |
Definition at line 452 of file qprogressdialog.cpp.
References d.
Referenced by HttpWindow::updateDataReadProgress(), and FtpWindow::updateDataTransferProgress().
00453 { 00454 Q_D(QProgressDialog); 00455 d->bar->setMaximum(maximum); 00456 }
| void QProgressDialog::setMinimum | ( | int | minimum | ) | [slot] |
Definition at line 473 of file qprogressdialog.cpp.
References d.
00474 { 00475 Q_D(QProgressDialog); 00476 d->bar->setMinimum(minimum); 00477 }
| void QProgressDialog::setValue | ( | int | progress | ) | [slot] |
Definition at line 561 of file qprogressdialog.cpp.
References d, QCoreApplication::flush(), h, QWidget::height(), QWidget::isModal(), QWidget::isVisible(), maximum(), minimum(), minWaitTime, qApp, qMax(), reset(), QWidget::resize(), QWidget::show(), sizeHint(), w, and QWidget::width().
Referenced by Window::findFiles(), MainWindow::printImage(), BatchTranslationDialog::startTranslation(), HttpWindow::updateDataReadProgress(), and FtpWindow::updateDataTransferProgress().
00562 { 00563 Q_D(QProgressDialog); 00564 if (progress == d->bar->value() || 00565 d->bar->value() == -1 && progress == d->bar->maximum()) 00566 return; 00567 00568 d->bar->setValue(progress); 00569 00570 if (d->shown_once) { 00571 if (isModal()) 00572 qApp->processEvents(); 00573 } else { 00574 if (progress == 0) { 00575 d->starttime.start(); 00576 d->forceTimer->start(d->showTime); 00577 return; 00578 } else { 00579 bool need_show; 00580 int elapsed = d->starttime.elapsed(); 00581 if (elapsed >= d->showTime) { 00582 need_show = true; 00583 } else { 00584 if (elapsed > minWaitTime) { 00585 int estimate; 00586 int totalSteps = maximum() - minimum(); 00587 int myprogress = progress - minimum(); 00588 if ((totalSteps - myprogress) >= INT_MAX / elapsed) 00589 estimate = (totalSteps - myprogress) / myprogress * elapsed; 00590 else 00591 estimate = elapsed * (totalSteps - myprogress) / myprogress; 00592 need_show = estimate >= d->showTime; 00593 } else { 00594 need_show = false; 00595 } 00596 } 00597 if (need_show) { 00598 int w = qMax(isVisible() ? width() : 0, sizeHint().width()); 00599 int h = qMax(isVisible() ? height() : 0, sizeHint().height()); 00600 resize(w, h); 00601 show(); 00602 d->shown_once = true; 00603 } 00604 } 00605 #ifdef Q_WS_MAC 00606 QApplication::flush(); 00607 #endif 00608 } 00609 00610 if (progress == d->bar->maximum() && d->autoReset) 00611 reset(); 00612 }
| void QProgressDialog::setLabelText | ( | const QString & | ) | [slot] |
Definition at line 336 of file qprogressdialog.cpp.
References d, h, QWidget::height(), QWidget::isVisible(), qMax(), QWidget::resize(), sizeHint(), w, and QWidget::width().
Referenced by FtpWindow::downloadFile(), HttpWindow::downloadFile(), and Window::findFiles().
00337 { 00338 Q_D(QProgressDialog); 00339 if (d->label) { 00340 d->label->setText(text); 00341 int w = qMax(isVisible() ? width() : 0, sizeHint().width()); 00342 int h = qMax(isVisible() ? height() : 0, sizeHint().height()); 00343 resize(w, h); 00344 } 00345 }
| void QProgressDialog::setCancelButtonText | ( | const QString & | cancelButtonText | ) | [slot] |
Sets the cancel button's text to cancelButtonText.
Definition at line 385 of file qprogressdialog.cpp.
References d, h, QWidget::height(), QString::isNull(), QWidget::isVisible(), qMax(), QDialog::QPushButton, QWidget::resize(), setCancelButton(), sizeHint(), w, and QWidget::width().
Referenced by Window::findFiles().
00386 { 00387 Q_D(QProgressDialog); 00388 if (!cancelButtonText.isNull()) { 00389 if (d->cancel) 00390 d->cancel->setText(cancelButtonText); 00391 else 00392 setCancelButton(new QPushButton(cancelButtonText, this)); 00393 } else { 00394 setCancelButton(0); 00395 } 00396 int w = qMax(isVisible() ? width() : 0, sizeHint().width()); 00397 int h = qMax(isVisible() ? height() : 0, sizeHint().height()); 00398 resize(w, h); 00399 }
| void QProgressDialog::setMinimumDuration | ( | int | ms | ) | [slot] |
Definition at line 666 of file qprogressdialog.cpp.
References d.
00667 { 00668 Q_D(QProgressDialog); 00669 d->showTime = ms; 00670 if (d->bar->value() == 0) { 00671 d->forceTimer->stop(); 00672 d->forceTimer->start(ms); 00673 } 00674 }
| void QProgressDialog::canceled | ( | ) | [signal] |
This signal is emitted when the cancel button is clicked. It is connected to the cancel() slot by default.
Referenced by closeEvent(), and setCancelButton().
| void QProgressDialog::resizeEvent | ( | QResizeEvent * | ) | [protected, virtual] |
Reimplemented from QDialog.
Definition at line 635 of file qprogressdialog.cpp.
References d.
00636 { 00637 Q_D(QProgressDialog); 00638 d->layout(); 00639 }
| void QProgressDialog::closeEvent | ( | QCloseEvent * | e | ) | [protected, virtual] |
Reimplemented from QDialog.
Definition at line 687 of file qprogressdialog.cpp.
References canceled(), QDialog::closeEvent(), and emit.
00688 { 00689 emit canceled(); 00690 QDialog::closeEvent(e); 00691 }
Here is the call graph for this function:

| void QProgressDialog::changeEvent | ( | QEvent * | ev | ) | [protected, virtual] |
Reimplemented from QWidget.
Definition at line 644 of file qprogressdialog.cpp.
References QWidget::changeEvent(), d, QEvent::StyleChange, and QEvent::type().
00645 { 00646 Q_D(QProgressDialog); 00647 if(ev->type() == QEvent::StyleChange) 00648 d->layout(); 00649 QDialog::changeEvent(ev); 00650 }
Here is the call graph for this function:

| void QProgressDialog::showEvent | ( | QShowEvent * | e | ) | [protected, virtual] |
Reimplemented from QDialog.
Definition at line 739 of file qprogressdialog.cpp.
References d, h, QWidget::height(), QWidget::isVisible(), qMax(), QWidget::resize(), QDialog::showEvent(), sizeHint(), w, and QWidget::width().
00740 { 00741 Q_D(QProgressDialog); 00742 QDialog::showEvent(e); 00743 int w = qMax(isVisible() ? width() : 0, sizeHint().width()); 00744 int h = qMax(isVisible() ? height() : 0, sizeHint().height()); 00745 resize(w, h); 00746 d->forceTimer->stop(); 00747 }
Here is the call graph for this function:

| void QProgressDialog::forceShow | ( | ) | [protected, slot] |
Shows the dialog if it is still hidden after the algorithm has been started and minimumDuration milliseconds have passed.
Definition at line 756 of file qprogressdialog.cpp.
References d, and QWidget::show().
00757 { 00758 Q_D(QProgressDialog); 00759 if (d->shown_once || d->cancellation_flag) 00760 return; 00761 00762 show(); 00763 d->shown_once = true; 00764 }
1.5.1