在两个 类 "QWidget" 之间共享属性

Share attribute between two classes "QWidget"

我正在尝试将属性从 QWidget class“发送”到另一个。

在下面的示例中,我尝试将属于 class“Widget1”的 QLineEdit“self.edit”的文本设置为 QLabel“self.label”的文本”属于 class“Widget2”。

此尝试是在函数“setLabel”中进行的。 我无法弄清楚的部分是“Widget2.label.setText(text)”

在一个函数的 class 中有一个 class...我有点困惑如何实现它...

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()

任何帮助将不胜感激,如果我的示例或解释不清楚,我将提供进一步的解释。

您可以使用自定义信号执行此操作。

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()

文档:https://doc.qt.io/qt-5/signalsandslots.html