pyside6 子窗口给主窗口传参(信号)
思路
和主窗口给子窗口传参数不同的点就是需要把,主窗口传入子窗口
代码
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QLineEdit
from PySide6.QtCore import Signal, Slot
class Mywindow(QWidget):
def __init__(self):
super().__init__()
self.mainLayout = QVBoxLayout()
self.label = QLabel('主窗口:')
self.IPLineEdit = QLineEdit()
self.mainLayout.addWidget(self.label)
self.mainLayout.addWidget(self.IPLineEdit)
self.setLayout(self.mainLayout)
self.bind()
def bind(self):
self.subwindow = Subwindow(self)
self.subwindow.show()
class Subwindow(QWidget):
sendValueTomain = Signal(str) # 0.创建一个信号,并且带一个字符串类型参数
def __init__(self, mainwindow): # 这里接受主窗口
super().__init__()
self.mainwindow = mainwindow
self.mainLayout = QVBoxLayout()
self.label = QLabel("子窗口")
self.sublineedit = QLineEdit()
self.sendbtn = QPushButton('发送给主窗口')
self.mainLayout.addWidget(self.label)
self.mainLayout.addWidget(self.sublineedit)
self.mainLayout.addWidget(self.sendbtn)
self.setLayout(self.mainLayout)
self.bind()
def bind(self):
self.sendbtn.clicked.connect(self.sendvalue)
self.sendValueTomain.connect(self.mainwindow.IPLineEdit.setText) # 子窗口信号绑定修改主窗口的值
def sendvalue(self):
text = self.sublineedit.text()
print(text)
self.sendValueTomain.emit(text)
if __name__ == '__main__':
app = QApplication([])
window = Mywindow()
window.show()
app.exec()