I have a problem with my code. My plan is to show the progress of a for loop using a progressbar. My idea was to use Qthread. My code below works somehow, but not 100 percently correct. The progressbar shows the progress of the for loop, but not via a thread i.e. if I try to click more then once on Stop the GUI freezes. I am not a QtCore expert. Can please someone help me and tell me why it does not work the way I want it to work?
Thanks a lot!
from PyQt4 import QtGui, QtCore
#Progressbar
class MyCustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyCustomWidget, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
self.progressBar = QtGui.QProgressBar(self)
self.progressBar.setRange(0,100)
layout.addWidget(self.progressBar)
#Update Progressbar
def onProgress(self, i):
self.progressBar.setValue(i)
if self.progressBar.value() >= self.progressBar.maximum():
self.close()
#Threading Class
class ASA(QtCore.QThread):
notifyProgress = QtCore.pyqtSignal(int)
def run(self, i):
#Sends the new information to the Update Function
self.notifyProgress.emit(i)
time.sleep(0.01)
#-----------------------------------------#
#Main Function
app = QtGui.QApplication(sys.argv)
bar = MyCustomWidget()
bar.show()
bar.asa = ASA()
bar.asa.notifyProgress.connect(bar.onProgress)
bar.asa.start()
#For loop for the progressbar
for i in range(105):
ASA.run(bar.asa, i)
time.sleep(0.5)
sys.exit(app.exec_())
The loop needs to be run inside the thread itself:
def run(self):
#Sends the new information to the Update Function
for i in range(105):
self.notifyProgress.emit(i)
time.sleep(0.01)