PYQT and progress Bar during Long Process

2019-04-15 15:22发布

问题:

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.

回答1:

well, to have the test() method work while the progress bar is spinning you can do:

class TaskThread(QtCore.QThread):
    taskFinished = QtCore.pyqtSignal()
    def run(self):
        # instead of sleeping do the long running loop
        for i in xrange(500000):
            print i
        self.taskFinished.emit()  

and remove the test() method from Ui_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.



回答2:

The test() method has nothing to do with the progress bar, as you've currently designed it. The TaskThread.run() method must emit some signal with the current progress of its long-running operation. This signal must be connected to the slot progressBar.setValue(). Thus, when the operation in the TaskThread 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

class Ui_Form():
    __init__(self, Form):

        # Omitted

        # Connect progress of task thread with progress bar
        myLongTask.valueChanged.connect(self.progressBar.setValue)

    def onStart(self):
        # QProgressBar uses integers to represent range and value
        self.progressBar.setRange(0, 100)
        self.myLongTask.start()

class TaskThread():
    valueChanged = QtCore.pyqtSignal(int)

    def run():
        for i in range(100):
            time.sleep(0.01) # Do "work"
            self.valueChanged.emit(i) # Notify progress bar to update via signal

        self.taskFinished.emit()