PyQt designer promoted widget - reference to top level object?

615 Views Asked by At

In Qt designer, I have a custom graphicsView widget which I promote to a custom class called View:

enter image description here

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?

1

There are 1 best solutions below

1
On BEST ANSWER

Actually, the parent of your View is horizontalLayoutWidget, which is a QWidget - 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).