pyside6通过信号发送字符串到子窗口
【代码】pyside6通过信号发送字符串到子窗口。
·
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()
更多推荐
所有评论(0)