PySide.QtGui.QProgressDialog 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. PySide.QtGui.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 PySide.QtGui.QProgressDialog.minimumDuration() (4 seconds by default).
使用 PySide.QtGui.QProgressDialog.setMinimum() and PySide.QtGui.QProgressDialog.setMaximum() or the constructor to set the number of “steps” in the operation and call PySide.QtGui.QProgressDialog.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 PySide.QtGui.QProgressDialog.setMinimum() , and the progress dialog shows that the operation has finished when you call PySide.QtGui.QProgressDialog.setValue() with the value set by PySide.QtGui.QProgressDialog.setMaximum() as its argument.
对话框在操作结束时自动重置并隐藏本身。使用 PySide.QtGui.QProgressDialog.setAutoReset() and PySide.QtGui.QProgressDialog.setAutoClose() to change this behavior. Note that if you set a new maximum (using PySide.QtGui.QProgressDialog.setMaximum() or PySide.QtGui.QProgressDialog.setRange() ) that equals your current PySide.QtGui.QProgressDialog.value() , the dialog will not close regardless.
There are two ways of using PySide.QtGui.QProgressDialog : modal and modeless.
Compared to a modeless PySide.QtGui.QProgressDialog , a modal PySide.QtGui.QProgressDialog is simpler to use for the programmer. Do the operation in a loop, call PySide.QtGui.QProgressDialog.setValue() at intervals, and check for cancellation with PySide.QtGui.QProgressDialog.wasCanceled() 。例如:
progress = QProgressDialog("Copying files...", "Abort Copy", 0, numFiles, self)
progress.setWindowModality(Qt.WindowModal)
for i in rang(numFiles):
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 PySide.QtCore.QTimer (或 QObject.timerEvent() ), PySide.QtCore.QSocketNotifier ,或 QUrlOperator ; or performed in a separate thread. A PySide.QtGui.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 PySide.QtGui.QProgressDialog.canceled() signal to a slot that stops the operation, and call PySide.QtGui.QProgressDialog.setValue() at intervals. For example:
# Operation constructor
def __init__(self, parent):
QObject.__init__(self, parent)
pd = QProgressDialog("Operation in progress.", "Cancel", 0, 100)
connect(pd, SIGNAL("canceled()"), self, SLOT("cancel()"))
t = QTimer(self)
connect(t, SIGNAL("timeout()"), self, SLOT("perform()"))
t.start(0)
def perform(self):
pd.setValue(steps)
#... perform one percent of the operation
steps++
if steps > pd.maximum():
t.stop()
def cancel(self):
t.stop()
#... cleanup
In both modes the progress dialog may be customized by replacing the child widgets with custom widgets by using PySide.QtGui.QProgressDialog.setLabel() , PySide.QtGui.QProgressDialog.setBar() ,和 PySide.QtGui.QProgressDialog.setCancelButton() . The functions PySide.QtGui.QProgressDialog.setLabelText() and PySide.QtGui.QProgressDialog.setCancelButtonText() set the texts shown.
另请参阅
PySide.QtGui.QDialog PySide.QtGui.QProgressBar GUI 设计手册:进度指示器 查找文件范例 像素器范例
| 参数: |
|
|---|
| 返回类型: | PySide.QtCore.bool |
|---|
此特性保持对话框是否被隐藏,通过 PySide.QtGui.QProgressDialog.reset() .
默认为 true。
| 返回类型: | PySide.QtCore.bool |
|---|
此特性保持进度对话框是否调用 PySide.QtGui.QProgressDialog.reset() as soon as PySide.QtGui.QProgressDialog.value() 等于 PySide.QtGui.QProgressDialog.maximum() .
默认为 true。
重置进度对话框。 PySide.QtGui.QProgressDialog.wasCanceled() becomes true until the progress dialog is reset. The progress dialog becomes hidden.
展示对话框,若算法被启动后其仍被隐藏和 PySide.QtGui.QProgressDialog.minimumDuration() 毫秒已过去。
| 返回类型: | unicode |
|---|
This property holds the label's text.
默认文本为空字符串。
| 返回类型: | PySide.QtCore.int |
|---|
This property holds the highest value represented by the progress bar.
默认为 0。
| 返回类型: | PySide.QtCore.int |
|---|
This property holds the lowest value represented by the progress bar.
默认为 0。
| 返回类型: | PySide.QtCore.int |
|---|
This property holds the time that must pass before the dialog appears.
If the expected duration of the task is less than the PySide.QtGui.QProgressDialog.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 PySide.QtGui.QProgressDialog.minimumDuration() , the dialog will pop up after the PySide.QtGui.QProgressDialog.minimumDuration() time or as soon as any progress is set.
若设为 0,始终一设置任何进度,就会尽快展示对话框。默认为 4000 毫秒。
| 参数: |
|
|---|
这是重载函数。
打开对话框并连接其 PySide.QtGui.QDialog.accepted() signal to the slot specified by receiver and member .
信号将断开槽连接,当对话框被关闭时。
重置进度对话框。进度对话框变为隐藏,若 PySide.QtGui.QProgressDialog.autoClose() 为 true。
| 参数: | close – PySide.QtCore.bool |
|---|
此特性保持对话框是否被隐藏,通过 PySide.QtGui.QProgressDialog.reset() .
默认为 true。
| 参数: | reset – PySide.QtCore.bool |
|---|
此特性保持进度对话框是否调用 PySide.QtGui.QProgressDialog.reset() as soon as PySide.QtGui.QProgressDialog.value() 等于 PySide.QtGui.QProgressDialog.maximum() .
默认为 true。
| 参数: | bar – PySide.QtGui.QProgressBar |
|---|
将进度栏小部件设为 bar 。进度对话框会重置尺寸以拟合。进度对话框拥有其所有权对于进度 bar 当必要时会被删除,因此不要使用分配在堆栈上的进度条。
| 参数: | button – PySide.QtGui.QPushButton |
|---|
将取消按钮设为 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. If 0 is passed then no cancel button will be shown.
| 参数: | text – unicode |
|---|
将取消按钮的文本设为 cancelButtonText . If the text is set to QString() then it will cause the cancel button to be hidden and deleted.
| 参数: | label – PySide.QtGui.QLabel |
|---|
把标签设为 label 。进度对话框会重置尺寸以拟合。标签变为由进度对话框所有,且当必要时会将其删除,因此,不要把对象地址传递到堆栈上。
| 参数: | text – unicode |
|---|
This property holds the label's text.
默认文本为空字符串。
| 参数: | maximum – PySide.QtCore.int |
|---|
This property holds the highest value represented by the progress bar.
默认为 0。
| 参数: | minimum – PySide.QtCore.int |
|---|
This property holds the lowest value represented by the progress bar.
默认为 0。
| 参数: | ms – PySide.QtCore.int |
|---|
This property holds the time that must pass before the dialog appears.
If the expected duration of the task is less than the PySide.QtGui.QProgressDialog.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 PySide.QtGui.QProgressDialog.minimumDuration() , the dialog will pop up after the PySide.QtGui.QProgressDialog.minimumDuration() time or as soon as any progress is set.
若设为 0,始终一设置任何进度,就会尽快展示对话框。默认为 4000 毫秒。
| 参数: |
|
|---|
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 PySide.QtGui.QProgressDialog.reset() .
| 参数: | progress – PySide.QtCore.int |
|---|
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 to QProgressDialog.maximum() ; you can call PySide.QtGui.QProgressDialog.setValue() any number of times in-between.
警告
若进度对话框是模态的 (见 QProgressDialog.QProgressDialog() ), PySide.QtGui.QProgressDialog.setValue() calls QApplication.processEvents() , so take care that this does not cause undesirable re-entrancy in your code. For example, don't use a PySide.QtGui.QProgressDialog inside a PySide.QtGui.QWidget.paintEvent() !
| 返回类型: | PySide.QtCore.int |
|---|
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 to QProgressDialog.maximum() ; you can call PySide.QtGui.QProgressDialog.setValue() any number of times in-between.
警告
若进度对话框是模态的 (见 QProgressDialog.QProgressDialog() ), PySide.QtGui.QProgressDialog.setValue() calls QApplication.processEvents() , so take care that this does not cause undesirable re-entrancy in your code. For example, don't use a PySide.QtGui.QProgressDialog inside a PySide.QtGui.QWidget.paintEvent() !
| 返回类型: | PySide.QtCore.bool |
|---|
This property holds whether the dialog was canceled.