I am trying to make a progress bar to work on my very simple test app in pyqt but unfortunately I do not have any idea on how to implement some of the examples I saw as they go over my head so please bear with my newbie question.
Let's say I have a very simple QMainWindow
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(750, 450, 400, 200)
self.setFixedSize(self.size())
btn1 = QtGui.QPushButton("Convert", self)
btn1.move(210,171)
btn1.clicked.connect(self.output_convert)
def output_convert(self):
#very long process to convert a txt file to excel
#Here is where I would like to implement a progress bar
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
app.exec_()
run()
How can I implement at least a pulsing progress bar in the output_convert
?
What really confuses me is how do I call/execute the codes in the example. Can I do it without subclassing and purely in a function? does it depend in the output_convert
code? TLDR: How can I Implement a progress bar while executing a very long code (newbie friendly)?
UPDATE 1 After a whole day I got it working partially here's my code
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(750, 450, 400, 200)
self.setFixedSize(self.size())
btn1 = QtGui.QPushButton("Convert", self)
btn1.move(210,171)
btn1.clicked.connect(self.progbar)
def progbar (self):
self.prog_win = QDialog()
self.prog_win.resize(400, 100)
self.prog_win.setFixedSize(self.prog_win.size())
self.prog_win.setWindowTitle("Processing request")
self.lbl = QLabel(self.prog_win)
self.lbl.setText("Please Wait. . .")
self.lbl.move(15,18)
self.progressBar = QtGui.QProgressBar(self.prog_win)
self.progressBar.resize(410, 25)
self.progressBar.move(15, 40)
self.progressBar.setRange(0,1)
self.myLongTask = TaskThread()
#I think this is where I am wrong
self.prog_win.show()
self.myLongTask.taskFinished.connect(self.onStart)
self.output_settings()
def onStart(self):
self.progressBar.setRange(0,0)
self.myLongTask.start()
def output_convert(self):
#very long process to convert a txt file to excel
class TaskThread(QtCore.QThread):
taskFinished = QtCore.pyqtSignal()
def run(self):
time.sleep(3)
self.taskFinished.emit()
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
app.exec_()
run()
I can now see what I lack and what I really need is Threading. I read around and everyone said that it needs to multithread and subclassing is a bad idea. It partially works as the progress bar hangs while processing.