问题描述
我有一个由 Qt 设计器创建的 GUI 类,其中有一个进度条,另一个类在其中完成了所有数字运算,在此期间我希望我的进度条定期更新.我认为我会这样做的方式是在其他班级中做这样的事情:
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%)
但我似乎无法做到这一点.gui 类的代码类似于:
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
推荐答案
gui.Ui_mainLayout
不是一个实例化的类,而是一个类型"对象(一个可以实例化的对象 - 见 此处 以获得很好的概述).gui.Ui_mainLayout.progressBar
在 setupUi
运行时不会像它创建的那样存在.
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.
尝试将 progressBar
显式传递给 RunProgram
:
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()
和
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.
这篇关于从我的 GUI 类 PyQt4 以外的类更改进度条的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!