John
John

Reputation: 99

Share attribute between two classes "QWidget"

I'm trying to "send" the attribute from a QWidget class to another.

In the example below, I'm trying to set the text of the QLineEdit "self.edit" belonging to the class "Widget1" as text of the QLabel "self.label" belonging to the class "Widget2".

This attempt is made in the function "setLabel". The part that I cannot figure out is "Widget2.label.setText(text)"

Having a class in a class in a function... I'm a little bit confused how to achieve that...

import sys
from PySide2.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QLabel, QLineEdit)

class Main_UI(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()
        widget1 = Widget1()
        widget2 = Widget2()
        layout.addWidget(widget1)
        layout.addWidget(widget2)
        self.setLayout(layout)
        self.show()

class Widget1(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        layout = QHBoxLayout()
        self.edit = QLineEdit("")
        button = QPushButton("Set value")
        button.clicked.connect(self.setLabel)
        layout.addWidget(self.edit)
        layout.addWidget(button)
        self.setLayout(layout)
    
    def setLabel(self):
        text = self.edit.text()
        Widget2.label.setText(text)

class Widget2(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        layout = QHBoxLayout()
        self.label = QLabel("")
        layout.addWidget(self.label)
        self.setLayout(layout)

def main():
    app = QApplication(sys.argv)
    ex = Main_UI()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Any help would be appreciated, and if my example or explanations are not clear, I'll provide further explanations.

Upvotes: 1

Views: 232

Answers (1)

max wiklund
max wiklund

Reputation: 113

You can do this with a custom signal.

import sys
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QLabel, QLineEdit)
from PyQt5 import QtCore


class Main_UI(QWidget):
    def __init__(self, parent=None):
        super(Main_UI, self).__init__(parent)
        self.initUI()
    
    def initUI(self):
        layout = QVBoxLayout()
        widget1 = Widget1()
        widget2 = Widget2()
        layout.addWidget(widget1)
        layout.addWidget(widget2)
        self.setLayout(layout)
        
        widget1.button_signal.connect(widget2.label.setText)  # Connecting the label to the custom signal.
        
        self.show()


class Widget1(QWidget):
    button_signal = QtCore.pyqtSignal(str)  # Creating a signal.
    
    def __init__(self, parent=None):
        super(Widget1, self).__init__(parent)
        self.initUI()
    
    def initUI(self):
        layout = QHBoxLayout()
        self.edit = QLineEdit("")
        button = QPushButton("Set value")
        button.clicked.connect(self.setLabel)
        layout.addWidget(self.edit)
        layout.addWidget(button)
        self.setLayout(layout)
    
    def setLabel(self):
        """Emit button signal with text.
        
        This could have been solved with a lambda.
        
        """        
        self.button_signal.emit(self.edit.text())  # Emitting Signal.


class Widget2(QWidget):
    def __init__(self, parent=None):
        super(Widget2, self).__init__(parent)
        self.initUI()
    
    def initUI(self):
        layout = QHBoxLayout()
        self.label = QLabel("")
        layout.addWidget(self.label)
        self.setLayout(layout)


def main():
    app = QApplication(sys.argv)
    ex = Main_UI()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Docs: https://doc.qt.io/qt-5/signalsandslots.html

Upvotes: 2

Related Questions