The Q3ProgressDialog class provides feedback on the progress of a slow operation. 更多...
| 头: | #include <Q3ProgressDialog> |
| 继承: | 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 ) |
| void | canceled () |
| void | cancelled () |
| virtual void | changeEvent (QEvent * ev ) |
| virtual void | closeEvent (QCloseEvent * e ) |
| virtual void | resizeEvent (QResizeEvent *) |
| virtual void | showEvent (QShowEvent * e ) |
| void | forceShow () |
The Q3ProgressDialog class provides feedback on the progress of a slow 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 beyond minimumDuration () (默认 4 秒)。
使用 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 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 call setProgress () 采用 totalSteps () 作为其自变量。
对话框在操作结束时自动重置并隐藏本身。使用 setAutoReset () 和 setAutoClose () to change this behavior.
There are two ways of using Q3ProgressDialog : modal and modeless.
Using a modal Q3ProgressDialog is simpler for the programmer, but you must call QApplication::processEvents () 或 QEventLoop::processEvents (ExcludeUserInput) to keep the event loop running to ensure that the application doesn't freeze. Do the operation in a loop, call setProgress () 不时,并校验是否取消采用 wasCanceled ()。例如:
Q3ProgressDialog progress("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 on QTimer (或 QObject::timerEvent ()), QSocketNotifier ,或 QUrlOperator ;或在单独线程中履行。 Q3ProgressBar 在主窗口状态栏中,经常是非模态进度对话框的替代。
需要以事件循环来运行,连接 canceled () 信号到操作停止槽,和调用 setProgress () 不时。例如:
Operation::Operation(QObject *parent = 0) : QObject(parent), steps(0) { pd = new Q3ProgressDialog("Operation in progress.", "Cancel", 100); connect(pd, SIGNAL(canceled()), 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 }
这 2 种模式,都可以通过采用自定义小部件替换子级 Widget 以定制进度对话框,通过使用 setLabel (), setBar (),和 setCancelButton ()。函数 setLabelText () 和 setCancelButtonText () 设置展示文本。
另请参阅 QDialog , Q3ProgressBar ,和 GUI 设计手册:进度指示器 .
This property holds whether the dialog gets hidden by reset().
默认为 true。
访问函数:
| bool | autoClose () const |
| void | setAutoClose (bool b ) |
另请参阅 setAutoReset ().
This property holds whether the progress dialog calls reset() as soon as progress() equals totalSteps().
默认为 true。
访问函数:
| bool | autoReset () const |
| void | setAutoReset (bool b ) |
另请参阅 setAutoClose ().
This property holds the label's text.
默认文本为空字符串。
访问函数:
| 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.
若设为 0,始终一设置任何进度,就会尽快展示对话框。默认为 4000 毫秒。
访问函数:
| int | minimumDuration () const |
| void | setMinimumDuration (int ms ) |
此特性保持当前已取得的进度数量。
For the progress dialog to work as expected, you should initially set this property to 0 and finally set it to Q3ProgressDialog::totalSteps (); you can call setProgress() any number of times in-between.
警告: 若进度对话框是模态的 (见 Q3ProgressDialog::Q3ProgressDialog ()), this function calls QApplication::processEvents (), so take care that this does not cause undesirable re-entrancy in your code. For example, don't use a Q3ProgressDialog 在 paintEvent ()!
访问函数:
| int | progress () const |
| void | setProgress (int progress ) |
| void | setProgress (int progress , int totalSteps ) |
另请参阅 totalSteps .
This property holds the total number of steps.
默认为 0。
访问函数:
| int | totalSteps () const |
| void | setTotalSteps (int totalSteps ) |
This property holds whether the dialog was canceled.
访问函数:
| bool | wasCanceled () const |
另请参阅 setProgress ().
This property holds whether the dialog was canceled.
使用 wasCanceled 代替。
该特性在 Qt 4.2 引入。
访问函数:
| bool | wasCancelled () const |
构造进度对话框。
默认设置:
The creator argument is the widget to use as the dialog's parent. The name , modal , and the widget flags, f , are passed to the QDialog::QDialog () constructor. If modal is false (the default), you must have an event loop proceeding for any redrawing of the dialog to occur. If modal is true, the dialog ensures that events are processed when needed.
另请参阅 setLabelText (), setLabel (), setCancelButtonText (), setCancelButton (),和 setTotalSteps ().
构造进度对话框。
The labelText is 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 totalSteps 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, call setProgress (0). As each file is processed call setProgress (1), setProgress (2), etc., finally calling setProgress (50) after examining the last file.
The creator argument is the widget to use as the dialog's parent. The name , modal , and widget flags, f , are passed to the QDialog::QDialog () constructor. If modal is false (the default), you will must have an event loop proceeding for any redrawing of the dialog to occur. If modal is true, the dialog ensures that events are processed when needed.
另请参阅 setLabelText (), setLabel (), setCancelButtonText (), setCancelButton (),和 setTotalSteps ().
构造进度对话框。
默认设置:
The creator argument is the widget to use as the dialog's parent. The widget flags, f , are passed to the QDialog::QDialog () 构造函数。
另请参阅 setLabelText (), setLabel (), setCancelButtonText (), setCancelButton (),和 setTotalSteps ().
构造进度对话框。
The labelText is 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 totalSteps 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, call setProgress (0). As each file is processed call setProgress (1), setProgress (2), etc., finally calling setProgress (50) after examining the last file.
The creator argument is the widget to use as the dialog's parent. The widget flags, f , are passed to the QDialog::QDialog () 构造函数。
另请参阅 setLabelText (), setLabel (), setCancelButtonText (), setCancelButton (),和 setTotalSteps ().
销毁进度对话框。
[slot]
void
Q3ProgressDialog::
cancel
()
重置进度对话框。 wasCanceled () 变为 true,直到进度对话框被重置。进度对话框变为隐藏。
[signal]
void
Q3ProgressDialog::
canceled
()
此信号发射,当点击取消按钮时。它被连接到 cancel () 槽,默认情况下。
另请参阅 wasCanceled ().
[signal]
void
Q3ProgressDialog::
cancelled
()
使用 canceled () 代替。
[virtual protected]
void
Q3ProgressDialog::
changeEvent
(
QEvent
*
ev
)
重实现自 QWidget::changeEvent ().
[virtual protected]
void
Q3ProgressDialog::
closeEvent
(
QCloseEvent
*
e
)
重实现自 QWidget::closeEvent ().
[protected slot]
void
Q3ProgressDialog::
forceShow
()
展示,若算法启动后对话框仍隐藏且 minimumDuration 毫秒已过去。
另请参阅 setMinimumDuration ().
[slot]
void
Q3ProgressDialog::
reset
()
重置进度对话框。进度对话框变为隐藏,若 autoClose () 为 true。
另请参阅 setAutoClose () 和 setAutoReset ().
[virtual protected]
void
Q3ProgressDialog::
resizeEvent
(
QResizeEvent
*)
重实现自 QWidget::resizeEvent ().
将进度栏小部件设为 bar 。进度对话框会重置大小以拟合。进度对话框拥有其所有权对于进度 bar 会被删除当有必要时,因此不要使用分配在堆栈中的进度条。
将取消按钮设为 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.
另请参阅 setCancelButtonText ().
[slot]
void
Q3ProgressDialog::
setCancelButtonText
(const
QString
&
cancelButtonText
)
将取消按钮的文本设为 cancelButtonText .
另请参阅 setCancelButton ().
把标签设为 label 。进度对话框会重置大小以拟合。标签变为由进度对话框所有,且会被删除当有必要时,因此,不要把对象地址传递给堆栈。
另请参阅 setLabelText ().
[virtual protected]
void
Q3ProgressDialog::
showEvent
(
QShowEvent
*
e
)
重实现自 QWidget::showEvent ().
[虚拟]
QSize
Q3ProgressDialog::
sizeHint
() const
重实现自 QWidget::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.