Pushing the button starts 100 rounds cycle. With QLabel.setText()
we update self.label
from inside of scope of clicked()
function.
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_()
Declare the progress bar as:
The code declares a local
progressbar
object connected to a custom 'customSignal` using:QtCore.QObject.connect(self, QtCore.SIGNAL("customSignal(int)"), progressbar, QtCore.SLOT("setValue(int)"))
with four arguments passed to
QtCore.QObject.connect()
.The first argument
self
is the object that will be emitting the signal. Since the function that will do the "sleep-every-second-processing" is declared under the mainDialog
instance we passself
here.The second argument is the name of the signal itself: 'customSignal'.
The
progressbar
object is used as third and its methodsetValue
as fourth last argument.--------------------
Here is variation of the same solution except linking to the progressbar method. import time
======================
This code now links a custom signal to a function referred as Slot in Qt.