PyQt5 使用 pyqtSignal 进行窗口数据传递

PyQt5 使用 pyqtSignal 进行窗口数据传递,第1张

示例1 dialog01.py
import sys
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QPushButton, QApplication, QWidget, QLineEdit
from PyQt5.QtCore import Qt
from dialog01 import Dialog


class Wind(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(400, 500)
        layout = QVBoxLayout()
        self.button1 = QPushButton('button1')
        self.button1.clicked.connect(self.show_dialog)
        self.edit = QLineEdit()

        widget = QWidget()
        layout.addWidget(self.button1)
        layout.addWidget(self.edit)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def name_edit(self, name):
        self.edit.setText(name)

    def show_dialog(self):
        print('show_dialog')
        dialog = Dialog(self)
        dialog.update_name.connect(self.name_edit)
        dialog.setWindowModality(Qt.WindowModal)
        dialog.show()
        print('end')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Wind()
    win.show()
    sys.exit(app.exec_())
dialog01.py
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QApplication
from PyQt5.QtCore import pyqtSignal
import sys


class Dialog(QDialog):
    update_name = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QVBoxLayout()
        label = QLabel('name')
        layout.addWidget(label)
        self.line_edit = QLineEdit()
        self.line_edit.setPlaceholderText('input name')
        self.line_edit.textChanged.connect(self.emit_name)
        layout.addWidget(self.line_edit)
        self.setLayout(layout)

    def emit_name(self):
        name = self.line_edit.text()
        self.update_name.emit(name)
效果

示例2 dialog01.py
import sys
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QPushButton, QApplication, QWidget, QLineEdit
from PyQt5.QtCore import Qt
from dialog01 import Dialog


class Wind(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(400, 500)
        layout = QVBoxLayout()
        self.button1 = QPushButton('button1')
        self.button1.clicked.connect(self.show_dialog)
        self.edit = QLineEdit()

        widget = QWidget()
        layout.addWidget(self.button1)
        layout.addWidget(self.edit)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def show_dialog(self):
        print('show_dialog')
        dialog = Dialog(self)
        dialog.setWindowModality(Qt.WindowModal)
        dialog.show()
        print('end')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Wind()
    win.show()
    sys.exit(app.exec_())
dialog01.py
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QApplication
from PyQt5.QtCore import pyqtSignal
import sys


class Dialog(QDialog):
    update_name = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        label = QLabel('name')
        layout.addWidget(label)
        self.line_edit = QLineEdit()
        self.line_edit.setPlaceholderText('input name')
        self.line_edit.textChanged.connect(self.emit_name)
        layout.addWidget(self.line_edit)
        self.setLayout(layout)

    def emit_name(self):
        name = self.line_edit.text()
        self.parent.edit.setText(name)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/876859.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存