I am trying to make a pulsating progress bar that runs during a long process I am running and when it is done it will completely turn green. I have read some previous post on here but I can't figure out the threading I need for this.
import time
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(770, 726)
self.progressBar = QtGui.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(150, 590, 118, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setTextVisible(False)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(25, 50, 170, 40))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.onStart)
self.myLongTask = TaskThread()
self.myLongTask.taskFinished.connect(self.onFinished)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def onStart(self):
self.progressBar.setRange(0,0)
self.myLongTask.start()
def onFinished(self):
# Stop the pulsation
self.progressBar.setRange(0,1)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "Run", None))
def test(self):
for i in xrange(500000):
print i
class TaskThread(QtCore.QThread):
taskFinished = QtCore.pyqtSignal()
def run(self):
time.sleep(3)
self.taskFinished.emit()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Right now the bar just pulsates. It never gets to the test def. i know this is because I am not calling it properly, but I am not sure how to have the bar pulsate and the def test run at the same time.
The
test()
method has nothing to do with the progress bar, as you've currently designed it. TheTaskThread.run()
method must emit some signal with the current progress of its long-running operation. This signal must be connected to the slotprogressBar.setValue()
. Thus, when the operation in theTaskThread
progresses a bit, a signal is sent to the progress bar to actually update with that new progress.Here's an outline, I've omitted a lot of the code for brevity
well, to have the
test()
method work while the progress bar is spinning you can do:and remove the
test()
method fromUi_form
. The whole idea being that you need to have the long running task within another thread than the main thread handling the main window.