QProgressDialog 類提供慢操作進度反饋。 更多...
| 頭: | #include <QProgressDialog> |
| qmake: | QT += widgets |
| 繼承: | QDialog |
|
| QProgressDialog (const QString & labelText , const QString & cancelButtonText , int minimum , int maximum , QWidget * parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) | |
| QProgressDialog (QWidget * parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) | |
| virtual | ~QProgressDialog () |
| bool | autoClose () const |
| bool | autoReset () const |
| QString | labelText () const |
| int | maximum () const |
| int | minimum () const |
| int | minimumDuration () const |
| void | open (QObject * receiver , const char * member ) |
| void | setAutoClose (bool close ) |
| void | setAutoReset (bool reset ) |
| void | setBar (QProgressBar * bar ) |
| void | setCancelButton (QPushButton * cancelButton ) |
| void | setLabel (QLabel * label ) |
| int | value () const |
| bool | wasCanceled () const |
| virtual QSize | sizeHint () const override |
| void | cancel () |
| void | reset () |
| void | setCancelButtonText (const QString & cancelButtonText ) |
| void | setLabelText (const QString & text ) |
| void | setMaximum (int maximum ) |
| void | setMinimum (int minimum ) |
| void | setMinimumDuration (int ms ) |
| void | setRange (int minimum , int maximum ) |
| void | setValue (int progress ) |
| void | canceled () |
| virtual void | changeEvent (QEvent * ev ) override |
| virtual void | closeEvent (QCloseEvent * e ) override |
| virtual void | resizeEvent (QResizeEvent * event ) override |
| virtual void | showEvent (QShowEvent * e ) override |
| void | forceShow () |
進度對話框用於給予用戶操作將花費多長時間的指示,並演示應用程序末被凍結。它還可以讓用戶,有機會中止操作。
進度對話框的常見問題是,很難知道將在何時使用它們;操作將花費不同數量的時間 , 在不同硬件。QProgressDialog 為此問題提供解決方案:它估計操作將花費的時間 (基於步進時間),且僅顯示自己若估計超過 minimumDuration () (默認 4 秒)。
使用 setMinimum () 和 setMaximum () 或構造函數,可以設置操作步數和調用 setValue () 作為操作進度。步數可以任選。它可以是拷貝文件數、收到字節數、透過主循環算法的迭代次數,或其它閤適單位。進度起始值的設置通過 setMinimum (),且進度對話框展示操作已完成,當調用 setValue () 采用值設置通過 setMaximum () 作為其自變量。
對話框在操作結束時自動重置並隱藏本身。使用 setAutoReset () 和 setAutoClose () 能改變這種行為。注意:若設置新的最大 (使用 setMaximum () 或 setRange ()) that equals your current value (),對話框將不會被關閉 (不管怎樣)。
有兩種使用 QProgressDialog 的辦法:模態和非模態。
比較非模態 QProgressDialog,模態 QProgressDialog 更易於使用對程序員而言。在循環中做操作,調用 setValue () 不時,並校驗是否取消采用 wasCanceled ()。例如:
QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
progress.setWindowModality(Qt::WindowModal);
for (int i = 0; i < numFiles; i++) {
progress.setValue(i);
if (progress.wasCanceled())
break;
//... copy one file
}
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 (或 QObject::timerEvent ()) 或 QSocketNotifier ;或在單獨綫程中履行。 QProgressBar 在主窗口狀態欄中,經常是非模態進度對話框的替代。
需要以事件循環來運行,連接 canceled () 信號到操作停止槽,和調用 setValue () 不時。例如:
// Operation constructor Operation::Operation(QObject *parent) : QObject(parent), steps(0) { pd = new QProgressDialog("Operation in progress.", "Cancel", 0, 100); connect(pd, &QProgressDialog::canceled, this, &Operation::cancel); t = new QTimer(this); connect(t, &QTimer::timeout, this, &Operation::perform); t->start(0); } void Operation::perform() { pd->setValue(steps); //... perform one percent of the operation steps++; if (steps > pd->maximum()) t->stop(); } void Operation::cancel() { t->stop(); //... cleanup }
這 2 種模式,都可以通過采用自定義小部件替換子級 Widget 以定製進度對話框,通過使用 setLabel (), setBar (),和 setCancelButton ()。函數 setLabelText () 和 setCancelButtonText () 設置展示文本。
另請參閱 QDialog , QProgressBar , GUI 設計手冊:進度指示器 , 查找文件範例 ,和 像素器範例 .
此特性保持對話框是否獲得隱藏,通過 reset ()
默認為 true。
訪問函數:
| bool | autoClose () const |
| void | setAutoClose (bool close ) |
另請參閱 setAutoReset ().
此特性保持進度對話框是否調用 reset () 盡快 value () 等於 maximum ()
默認為 true。
訪問函數:
| bool | autoReset () const |
| void | setAutoReset (bool reset ) |
另請參閱 setAutoClose ().
此特性保持標簽的文本
默認文本為空字符串。
訪問函數:
| QString | labelText () const |
| void | setLabelText (const QString & text ) |
此特性保持由進度條錶示的最高值
默認為 100。
訪問函數:
| int | maximum () const |
| void | setMaximum (int maximum ) |
此特性保持由進度條錶示的最低值
默認為 0。
訪問函數:
| int | minimum () const |
| void | setMinimum (int minimum ) |
此特性保持對話框齣現前,必須經過的時間
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.
若設為 0,始終一設置任何進度,就會盡快展示對話框。默認為 4000 毫秒。
訪問函數:
| int | minimumDuration () const |
| void | setMinimumDuration (int ms ) |
此特性保持當前已取得的進度數量。
為使進度對話框如期望般工作,應把此特性初始設為 QProgressDialog::minimum () 並把它最終設為 QProgressDialog::maximum ();可以調用 setValue() 任意多次在兩者之間。
警告: 若進度對話框是模態的 (見 QProgressDialog::QProgressDialog ()),setValue() 調用 QCoreApplication::processEvents (), so take care that this does not cause undesirable re-entrancy in your code. For example, don't use a QProgressDialog 在 paintEvent ()!
訪問函數:
| int | value () const |
| void | setValue (int progress ) |
此特性保持對話框是否被取消
訪問函數:
| bool | wasCanceled () const |
構造進度對話框。
The labelText 是用於提醒用戶進展如何的文本。
The cancelButtonText 是要在取消按鈕上顯示的文本。若傳遞的是 QString(),則不展示取消按鈕。
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 parent, parent , and widget flags, f , are passed to the QDialog::QDialog () 構造函數。
另請參閱 setLabelText (), setLabel (), setCancelButtonText (), setCancelButton (), setMinimum (),和 setMaximum ().
構造進度對話框。
默認設置:
The parent argument is dialog's parent widget. The widget flags, f , are passed to the QDialog::QDialog () 構造函數。
另請參閱 setLabelText (), setCancelButtonText (), setCancelButton (), setMinimum (),和 setMaximum ().
[slot]
void
QProgressDialog::
cancel
()
重置進度對話框。 wasCanceled () 變為 true,直到進度對話框被重置。進度對話框變為隱藏。
[signal]
void
QProgressDialog::
canceled
()
此信號發射,當點擊取消按鈕時。它被連接到 cancel () 槽,默認情況下。
另請參閱 wasCanceled ().
[protected slot]
void
QProgressDialog::
forceShow
()
展示,若算法啓動後對話框仍隱藏且 minimumDuration 毫秒已過去。
另請參閱 setMinimumDuration ().
[slot]
void
QProgressDialog::
reset
()
重置進度對話框。進度對話框變為隱藏,若 autoClose () 為 true。
另請參閱 setAutoClose () 和 setAutoReset ().
[slot]
void
QProgressDialog::
setCancelButtonText
(const
QString
&
cancelButtonText
)
將取消按鈕的文本設為 cancelButtonText 。若文本被設為 QString(),它將導緻取消按鈕被隱藏並被刪除。
另請參閱 setCancelButton ().
[slot]
void
QProgressDialog::
setRange
(
int
minimum
,
int
maximum
)
Sets the progress dialog's minimum and maximum values to minimum and maximum ,分彆。
若 maximum < minimum , minimum 變為唯一閤法值。
If the current value falls outside the new range, the progress dialog is reset with reset ().
[虛擬]
QProgressDialog::
~QProgressDialog
()
銷毀進度對話框。
[override virtual protected]
void
QProgressDialog::
changeEvent
(
QEvent
*
ev
)
重實現: QWidget::changeEvent (QEvent *event).
[override virtual protected]
void
QProgressDialog::
closeEvent
(
QCloseEvent
*
e
)
重實現: QDialog::closeEvent (QCloseEvent *e).
打開對話框並連接其 canceled () 信號到槽,指定通過 receiver and member .
將從槽斷開信號連接,當關閉對話框時。
該函數在 Qt 4.5 引入。
[override virtual protected]
void
QProgressDialog::
resizeEvent
(
QResizeEvent
*
event
)
重實現: QDialog::resizeEvent (QResizeEvent *).
將進度欄小部件設為 bar 。進度對話框會重置大小以擬閤。進度對話框擁有其所有權對於進度 bar 會被刪除當有必要時,因此不要使用分配在堆棧中的進度條。
將取消按鈕設為 Push Button (按鈕)
cancelButton
。進度對話框擁有此按鈕 (會被刪除當有必要時) 的所有權,因此,勿傳遞堆棧中的對象地址,即:使用 new() 去創建按鈕。若
nullptr
被傳遞,則取消按鈕不會被展示。
另請參閱 setCancelButtonText ().
把標簽設為 label 。進度對話框會重置大小以擬閤。標簽變為由進度對話框所有,且會被刪除當有必要時,因此,不要把對象地址傳遞給堆棧。
另請參閱 setLabelText ().
[override virtual protected]
void
QProgressDialog::
showEvent
(
QShowEvent
*
e
)
重實現: QDialog::showEvent (QShowEvent *event).
[override virtual]
QSize
QProgressDialog::
sizeHint
() const
重實現: QDialog::sizeHint () const.
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.