2

I have a GUI class created by Qt designer in which i have a progress bar, and another class in which all the number crunching is done during which i want my progress bar to update regularly. The way i thought i would do this it to do something like this in the other class:

gui.progressbar.setValue(some%)

but i cant seem to make that work. the code for the gui class is something like:

from PyQt4 import QtCore, QtGui
from Run import RunProgram

class Ui_mainLayout(QtGui.QWidget):
    def setupUi(self, mainLayout):
        mainLayout.setObjectName(_fromUtf8("mainLayout"))
        def setLayout():
            self.basic_tab = QtGui.QWidget()
            self.progressBar = QtGui.QProgressBar(self.basic_tab)
        setLayout()
        RunProgram()

i was then hoping to be able to do something like:

import gui

class RunProgram:
    def __init__(self):
        something = someMaths
        gui.Ui_mainLayout.progressBar.setValue(something)

but obviously as i am useless this doesnt work, could someone point me in the right direction? please and thank you

Ben
  • 5,525
  • 8
  • 42
  • 66

1 Answers1

1

gui.Ui_mainLayout is not an instantiated class but a 'type' object (an object that can be instantiated - see here for a good overview). gui.Ui_mainLayout.progressBar is not going to exist as its created when setupUi is run.

Try passing progressBar to RunProgram explicitly:

from PyQt4 import QtCore, QtGui
from Run import RunProgram

class Ui_mainLayout(QtGui.QWidget):
    def setupUi(self, mainLayout):
        mainLayout.setObjectName(_fromUtf8("mainLayout"))
        def setLayout():
            self.basic_tab = QtGui.QWidget()
            self.progressBar = QtGui.QProgressBar(self.basic_tab)
        setLayout(self.progressBar)
        RunProgram()

and

class RunProgram:
    def __init__(self, progressBar):
        something = someMaths
        progressBar.setValue(something)

I think that will work, but I suggest in future posting a minimal example you expect to run that can form the basis of the explanation.

Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
  • It's not quite that simple. You'll run into one of two issues: (1) the code is running in a different thread and you'll be trying to update cross-threads, which doesn't work; (2) it's running in the same thread and even though you call `setValue()` the progress bar won't update because the code is in a processing loop and the event loop hasn't had a chance to do the needed UI updates. There are ways around the latter but not the former. – Kaleb Pederson Jun 20 '11 at 20:07
  • As presented, its not all that clear what is wanted. Given that the setValue was in the init of the RunProgram, its not wholly unreasonable to assume that its required to initialise progress bar and so before the main loop has begun. Its why I suggested a minimal example to work from. But yes, I agree that given the whole story the best solution may be something completely different. – Henry Gomersall Jun 20 '11 at 22:34