In Qt designer, I have a custom graphicsView widget which I promote to a custom class called View:
Note that its parent is horizontalLayout. The corresponding code from pyuic looks like this:
class Ui_panelFill(object):
def setupUi(self, panelFill):
panelFill.setObjectName("panelFill")
panelFill.resize(1031, 702)
self.centralwidget = QtWidgets.QWidget(panelFill)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 1001, 631))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.graphicsView = View(self.horizontalLayoutWidget)
self.graphicsView.setObjectName("graphicsView")
...
The guts of the program, such as all the important variables and methods, are in a top level class called panelFill.
class panelFill(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
uic.loadUi("panelFill.ui",self)
self.vertices=[[0,0],[0,120],[240,120],[240,0]]
...
How can I reference the vertices variable from a method of the View class? I was hoping for something like self.parent.vertices but alas the parent is not the panelFill object, it's the horizontalLayout. Is there something like self.superduper().vertices to get to attributes of the top level parent object instead of the immediate parent object?
Actually, the parent of your
View
ishorizontalLayoutWidget
, which is aQWidget
- all widgets must have another widget as parent.To get the top-level widget in the hierarchy, you can use the window() method of your graphic-view widget. This method simply walks up the ancestor chain using parentWidget() until it finds a widget with no parent - which, by definition, must be the top-level widget (i.e. your main window).