0

How to update a Widget from Window A in Window B?
I have a widget that i want to change in my second window, how would i proceed from here?

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        #MyWidget
        ...

    def openSecond(self):
        self.SW = SecondWindow(#MyWidget)
        self.SW.show()


class SecondWindow(QMainWindow):
    def __init__(self, #MyWidget):
        super(SecondWindow, self).__init__()
        #MyWidget.changed
        #Update to parent
        ...
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Darjusch
  • 188
  • 13
  • did you put question and answer for your own question in the same moment ? – furas Oct 24 '19 at 12:37
  • Yes i encountered the problem today and didn't find a good source of information about it so after i solved it i wanted to answer it in the Q & A style. – Darjusch Oct 24 '19 at 12:41

1 Answers1

0

Pass the self from class A as Argument when creating an instance of the class B, then you can access the items from class A in class B with self.parent.

from PySide2.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.button = ''
        btn = QPushButton('Open Second', self)
        btn.move(10, 10)
        btn.clicked.connect(self.openSecond)

        self.resize(420, 450)

        button = QPushButton(self)
        button.move(100,100)
        button.setText("current text")
        self.button = button

    def openSecond(self):
        self.SW = SecondWindow(self.button, parent=self)
        self.SW.show()


class SecondWindow(QMainWindow):
    def __init__(self, button, parent=None):
        super(SecondWindow, self).__init__()
        self.parent = parent
        lbl = QLabel('Second Window', self)
        button.setText("updated text")
        self.parent.buttons = button


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    MW = MainWindow()
    MW.show()
    sys.exit(app.exec_())
Darjusch
  • 188
  • 13