uli_1973
uli_1973

Reputation: 721

PyQt widget seems to "forget" its parent

I have a strange issue when adding a widget to a PyQt5 application.

The following is the actual code, stripped off of everything that doesn't seem related (like translateUI):

class OllRoot(preferences.Group):
    """Basic openLilyLib installation"""
    def __init__(self, page):
        super(OllRoot, self).__init__(page)
        self.setParent(page)
        self.changedRoot()

        layout = QGridLayout()
        self.setLayout(layout)

        self.directory = widgets.urlrequester.UrlRequester()
        self.directory.changed.connect(self.changedRoot)

        layout.addWidget(self.directory, 0, 1)

    def changedRoot(self):
        print("Self:", self)
        print("Parent:", self.parent())
        self.parent().changed.emit()
        # TODO: Check for proper openLilyLib installation

When the constructor is called, parent() is correctly set to the object that has been passed in as page, so the two proper objects are printed.

Self: <preferences.openlilylib.OllRoot object at 0x7f855a1de288>
Parent: <preferences.openlilylib.OpenLilyLibPrefs object at 0x7f855a1bcb88>

However, when I make a change in the self.directory widget changedRoot is called again (as I've connected it), but now the parent seems to have disappeared:

Self: <preferences.openlilylib.OllRoot object at 0x7f855a1de288>
Parent: <PyQt5.QtWidgets.QWidget object at 0x7f855a1dbc18>

Question:

PS: A comparable file which served as a model can be found here: https://github.com/wbsoft/frescobaldi/blob/master/frescobaldi_app/preferences/general.py#L56.

Upvotes: 2

Views: 526

Answers (1)

ekhumoro
ekhumoro

Reputation: 120608

Whenever a widget is added to a layout, Qt will automatically re-parent it so that it becomes a child of the widget the layout is set on. Calling setParent (with a different widget) in __init__ will have no lasting effect.

Upvotes: 2

Related Questions