from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton
from PySide6.QtCore import Signal

class MyWindow(QWidget):
    sendValueToSubWindow = Signal(str)

    def __init__(self):
        super().__init__()
        self.setWindowTitle("主窗口")
        self.lineEditSend = QLineEdit()
        self.btn = QPushButton("发送数据到子窗口")

        self.mainLayout = QVBoxLayout()
        self.mainLayout.addWidget(self.lineEditSend)
        self.mainLayout.addWidget(self.btn)
        
        self.setLayout(self.mainLayout)
        self.bind()


    def bind(self):
        self.subWindow = SubWindow()
        self.subWindow.show()

        self.sendValueToSubWindow.connect(self.subWindow.lineEditSub.setText)
        self.btn.clicked.connect(self.sendValue)

    def sendValue(self):
        value = self.lineEditSend.text()
        self.sendValueToSubWindow.emit(value)

class SubWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("子窗口")
        # 接收
        self.lineEditSub = QLineEdit()
        
        self.mainLayout = QVBoxLayout()
        self.mainLayout.addWidget(self.lineEditSub)
        self.setLayout(self.mainLayout)

if __name__ == "__main__":
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐