如何使用 Signal 控制 QProgressBar [英] How to control QProgressBar with Signal
本文介绍了如何使用 Signal 控制 QProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按下按钮开始 100 轮循环.使用 QLabel.setText()
我们从 clicked()
函数的范围内更新 self.label
.
Pushing the button starts 100 rounds cycle. With QLabel.setText()
we update self.label
from inside of scope of clicked()
function.
除了更新self.label
,我们还想更新progressbar
.但是由于 progressbar
是一个局部变量,我们无法从 onClick()
函数内部更新它.
Aside from updating self.label
we would like to update the progressbar
as well.
But since progressbar
is a local variable we can't update it from inside of onClick()
function.
import time
class ProgressBar(QtGui.QProgressBar):
def __init__(self, parent=None, total=20):
super(ProgressBar, self).__init__(parent=parent)
self.setMinimum(1)
self.setMaximum(105)
self.setTextVisible(True)
def set_to_value(self, value):
self.setValue(value)
QtGui.qApp.processEvents()
def closeEvent(self, event):
self._active=False
class Dialog(QtGui.QDialog):
def __init__(self):
super(QtGui.QDialog,self).__init__()
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
self.label = QtGui.QLabel('idle...')
layout.addWidget(self.label)
progressbar = ProgressBar(self)
layout.addWidget(progressbar)
button = QtGui.QPushButton('Process')
button.clicked.connect(self.onClick)
layout.addWidget(button)
def onClick(self):
for i in range(101):
message = '...processing %s of 100'%i
self.label.setText(message)
QtGui.qApp.processEvents()
time.sleep(1)
if __name__ == '__main__':
app = QtGui.QApplication([])
dialog = Dialog()
dialog.resize(300, 100)
dialog.show()
app.exec_()
推荐答案
声明进度条为:
self.progressbar = ProgressBar(self)
这篇关于如何使用 Signal 控制 QProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文