Chaos
Chaos

Reputation: 57

Accessing parent attribute from inside child window

My main window has a variable self.x setup in the INIT. Then I need to access this value from the popup password box that is created. This is just a test script to get my point accross. I'm assuming it's something with the inheritance stuff, all that is still a little foreign to me.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PyQt5 import QtWidgets

# Created by MyWindow
class LoginDlg(QtWidgets.QDialog):

    def __init__(self):
        super(LoginDlg, self).__init__()

        self.password = QtWidgets.QLineEdit()
        # THIS IS THE LINE I NEED IT TO PULL X FROM PARENT
        self.password.setText(self.x)
        self.button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
        self.button_box.accepted.connect(self.accept)
        self.button_box.rejected.connect(self.reject)

        layout = QtWidgets.QFormLayout()
        layout.setFieldGrowthPolicy(QtWidgets.QFormLayout.AllNonFixedFieldsGrow)
        layout.addRow('Password', self.password)
        layout.addWidget(self.button_box)

        self.setLayout(layout)
        self.setWindowTitle("Login")
        self.setMinimumWidth(350)

# Main parent window
class MyWindow(QtWidgets.QWidget):

    def __init__(self):
        super(MyWindow, self).__init__()
        
        self.edit = QtWidgets.QLineEdit()
        button = QtWidgets.QPushButton("Get input from dialog")
        button.clicked.connect(self.get_login)
        layout = QtWidgets.QHBoxLayout()
        layout.addWidget(self.edit)
        layout.addWidget(button)
        self.setLayout(layout)
        self.x = "PASS THIS STRING"

    def get_login(self):
        login = LoginDlg()
        if login.exec_():
            self.edit.setText(login.password.text())


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

Normally I would just pass this data through the constructor, but let's say I have a lot of data that I don't want to pass back and forth, is there a way to access parent attributes another way?

Upvotes: 1

Views: 615

Answers (1)

eyllanesc
eyllanesc

Reputation: 243983

Don't get too complicated and just pass the value of that variable through the constructor:

class LoginDlg(QtWidgets.QDialog):
    def __init__(self, x):
        super(LoginDlg, self).__init__()

        self.password = QtWidgets.QLineEdit()
        self.password.setText(x)
        # ...
class MyWindow(QtWidgets.QWidget):
    # ...

    def get_login(self):
        login = LoginDlg(self.x)
        if login.exec_():
            self.edit.setText(login.password.text())

Another similar option is to access "password" in get_login:

class LoginDlg(QtWidgets.QDialog):
    def __init__(self):
        super(LoginDlg, self).__init__()

        self.password = QtWidgets.QLineEdit()
        # self.password.setText(self.x)
        self.button_box = QtWidgets.QDialogButtonBox(
            QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel
        )
        # ...
class MyWindow(QtWidgets.QWidget):
    # ...

    def get_login(self):
        login = LoginDlg()
        login.password.setText(self.x)
        if login.exec_():
            self.edit.setText(login.password.text())

Note: my answer does not try to implement what the OP asks is to "access a class from another" (there is no relationship so using parent-child is incorrect) since that complicates the modularization because if a object could affect the other, in general. I think the OP has an XY problem as it asks how to implement a possible solution instead of the underlying problem which is "how to share information between classes"

Upvotes: 2

Related Questions