1

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.

1 Answers1

1
        self.progressBar = QtWidgets.QProgressBar(self.centralWidget)
        self.progressBar.setGeometry(QtCore.QRect(150, 140, 481, 37))
        self.progressBar.setStyleSheet("your values")
        self.progressBar.setProperty("value", 70)
        self.progressBar.setObjectName("progressBar")

This is the code for the progress bar I have used, The first line defines the type of QtWidget, in our case the progress bar and it takes the argument, central widget, which shows that this will be a part of the central widget, set geometry is the positioning and the size. The set stylesheet is where you specify the colour and stuff. The set property function is used to set the value or the filling of the progressbar, this is what you want to vary.

Most of the time this widget is os dependent. That is, if you're using windows, you'll get the windows pulsing progress bar by default. But in other operating systems, for example linux, you will have to set the default styling of the widgets by using the setstyle() function.

Nevin Baiju
  • 305
  • 3
  • 18
  • I was not aware of `setstyle()` thank you as I was planning to distribute a cross-platform application. although this does not really answer my problem on how I would implement it in a very long process. do you have any sources that is easy to understand? – Juan Carlos Asuncion Jul 18 '17 at 00:26