Qt and progressBar - how to update

O

OMS

I am quite new to Python and Qt and need very urgently advice on how
to update Qt progressBar while executing a process. I have went thrugh
number of 'google' stuff and saw different solution, hence none worked
for me. The best idea I have seen is the usage of QThread and emiting
signal from thread to MainWindow. I do not know however why the code I
have written is not working. Most likely a stupid 'beginner related'
error but as far as I am beginner I can't get it. There is a code
below:

#!/opt/local/bin/python2.6

import os
import sys
import time
from PyQt4 import QtCore
from PyQt4 import QtGui

from uiTest import Ui_MainWindow

class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"),
self.runWorker)

def runWorker(self):
self.worker = Worker()
self.connect(self.worker, QtCore.SIGNAL("progressUpdated"),
self.updateWorkerProgress)
self.worker.start()

def updateWorkerProgress(self, min, max, progress):
self.ui.progressBar.setMinimum = min
self.ui.progressBar.setMaximum = max
self.ui.progressBar.setValue = progress
self.ui.progressBar.repaint()
#print min, progress, max

class Worker(QtCore.QThread):
__pyqtSignals__ = ("progressUpdated")
def __init__(self):
QtCore.QThread.__init__(self)
self.min = 0
self.max = 1000
self.progress = 0
def run(self):
for self.progress in range(self.min, self.max):
self.emit(QtCore.SIGNAL("progressUpdated"), self.min,
self.max, self.progress)
time.sleep(0.005)

def main():
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

if __name__ == "__main__":
main()

the uiTest is generated by pyuic4 and for reference can be found
below:

# Form implementation generated from reading ui file 'test.ui'
#
# Created: Sun May 16 19:54:59 2010
# by: PyQt4 UI code generator 4.7.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 40, 113, 32))
self.pushButton.setObjectName("pushButton")
self.progressBar = QtGui.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(50, 80, 431, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

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

def retranslateUi(self, MainWindow):

MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow",
"MainWindow", None, QtGui.QApplication.UnicodeUTF8))

self.pushButton.setText(QtGui.QApplication.translate("MainWindow",
"PushButton", None, QtGui.QApplication.UnicodeUTF8))
 
R

Robert Kern

I am quite new to Python and Qt and need very urgently advice on how
to update Qt progressBar while executing a process. I have went thrugh
number of 'google' stuff and saw different solution, hence none worked
for me. The best idea I have seen is the usage of QThread and emiting
signal from thread to MainWindow. I do not know however why the code I
have written is not working. Most likely a stupid 'beginner related'
error but as far as I am beginner I can't get it. There is a code
below:

#!/opt/local/bin/python2.6

import os
import sys
import time
from PyQt4 import QtCore
from PyQt4 import QtGui

from uiTest import Ui_MainWindow

class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"),
self.runWorker)

def runWorker(self):
self.worker = Worker()
self.connect(self.worker, QtCore.SIGNAL("progressUpdated"),
self.updateWorkerProgress)
self.worker.start()

def updateWorkerProgress(self, min, max, progress):
self.ui.progressBar.setMinimum = min
self.ui.progressBar.setMaximum = max
self.ui.progressBar.setValue = progress

These should be

self.ui.progressBar.setMinimum(min)
self.ui.progressBar.setMaximum(max)
self.ui.progressBar.setValue(progress)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
O

OMS

These should be

self.ui.progressBar.setMinimum(min)
self.ui.progressBar.setMaximum(max)
self.ui.progressBar.setValue(progress)

Thanks. You have saved the day. I knew it must be something only
greenhorn could do.

Once again thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,358
Latest member
TreyTritt8

Latest Threads

Top