0

I'm new to pyQt4 and i need to create GUI using pyQt4 and python 3.5. I have long process which does not have exact time duration to be executed. time duration is varying according to the inputs that I use in the process at each time. There are examples about adding progress bars by using timers.but in my case i don't know the exact time duration.

I need to complete a progress bar according to the time taken by the process.as in the my code this process is methodtobeexecute(). could someone help me out to resolve this problem with a code example with a explanation. Thanks in advance....

this is my code and so far it completes the progress bar using QTimer.but what i want is complete the progress bar meanwhile the methodtobeexecute() method finishes its tasks.(QTimer has used only for check the progrss bar here)

    # -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'pro2.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
import sys

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(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.progressBar = QtGui.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(160, 60, 118, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName(_fromUtf8("progressBar"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(170, 140, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        # # restarts the 'QProgressBar'
        self.progressBar.reset()

        # creates a 'QTimer'
        self.qtimer = QtCore.QTimer()

        # connects the 'QTimer.timeout()' signal with the 'qtimer_timeout()' slot
        self.qtimer.timeout.connect(self.qtimer_timeout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "start", None))
        self.pushButton.clicked.connect(self.qpushbutton_clicked)

    def qpushbutton_clicked(self):

        self.progressBar.reset()

        #this is the method that needs the time to complete the progress bar
        self.methodtobeexecute()

        self.qtimer.start(40)

    def qprogressbar_value_changed(self, value):
        if value == self.progressBar.maximum():
            # stops the 'QTimer'
            self.qtimer.stop()

    def qtimer_timeout(self):
        # gets the current value of the 'QProgressBar'
        value = self.progressBar.value()

        # adds 1 to the current value of the 'QProgressBar'
        self.progressBar.setValue(value + 1)

     def methodtobeexecute(self):
         for i in 400:
             print(i)





def main():
    app = QtGui.QApplication(sys.argv)
    mw = Ui_Form()
    mw.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
  • And... what have you tried so far? Please [EDIT](https://stackoverflow.com/posts/50941855/edit) your question and include your source code. Please do not post source code as comment to your own question. Thanks. – sɐunıɔןɐqɐp Jun 20 '18 at 06:52
  • Many times it is thought that the QProgressBar calculates the progress of a process, but the reality is that it does not, it only shows the progress we want, so your logic must calculate that progress, otherwise you can unfortunately be impossible, for example When you download a file many times you know in advance the kbs of the file so the advance is the amount of kbs downloaded compared to the total known. In your case as you indicate, you do not know it, so one option is to use the QProgressBar as an indicator that the process is running, but not as an indicator of progress. – eyllanesc Jun 20 '18 at 07:16
  • Example: `progressBar = QProgressBar()` `progressBar.setMinimum(0)` `progressBar.setMaximum(0)` – eyllanesc Jun 20 '18 at 07:20
  • If you do not know the total time you will not be able to get a percentage and therefore it will be impossible to indicate an advance. Do you understand that? QProgressBar does not magic – eyllanesc Jun 20 '18 at 07:44
  • What ***exactly*** does `methodtobeexecute` do? Your question is impossible to answer unless you state clearly what the long-running process does and how it works. Ideally, you should show the full code of the method in your question. – ekhumoro Jun 20 '18 at 11:41

0 Answers0