PyQT5 窗体采用Signal与Slot传参
1 基本步骤
(1)在主窗体定义子窗体,并定义连接信号的回调函数。
self.Frmgauss_fs = frmgauss_fs.Form()
#连接信号
self.Frmgauss_fs._signal.connect(self.getparameter)
注意子窗体必须先在init中定义,否则闪退,即使采用exec_()不闪退,但无法接收子窗体传来的信号;连接信号也必须在先定义,不能再主窗体按钮点击事件时定义,否则多次注册回调。
(2)在子窗体定义信号,信号设置数据类型如元组(列表等)
_signal = pyqtSignal(tuple)
(3)在子窗体按钮添加点击事件slot
self.btnCAL.clicked.connect(self.slot)
(4)在slot中发送信号
self._signal.emit(self.projectparam)
(4)在主窗体定义按钮调用子窗体获得信号
def btnOption_Click(self):
self.Frmgauss_fs.show()
def getparameter(self, parameter):
# parameter在子窗体的信号中定义的数据类型为元组 _signal = pyqtSignal(tuple)
# (a, rf, L, falseE, falseN)=projectparam
self.txtbox.setPlainText(str(parameter))
2 主窗体
import sys
from PyQt5.QtWidgets import (QDialog, QGridLayout, QMainWindow, QWidget,
QPushButton, QDesktopWidget, QSizePolicy,
QFileDialog, QMessageBox, QHBoxLayout,
QVBoxLayout, QApplication, QPlainTextEdit)
import frmgauss_fs
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
#主窗体按钮
self.btnOption = QPushButton("设置")
self.btnClose = QPushButton("关闭")
self.btnClose.clicked.connect(self.btnClose_Click)
self.btnOption.clicked.connect(self.btnOption_Click)
# self.txtbox.isReadOnly = True
# 按钮水平布局放在窗体右下方
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(self.btnOption)
hbox.addWidget(self.btnClose)
# 文本框
self.txtbox = QPlainTextEdit()
self.txtbox.setMinimumSize(200, 300)
# 主布局采用grid布局方式
maingridlayout = QGridLayout()
maingridlayout.addWidget(self.txtbox, 0, 0, 1, 1)
maingridlayout.addLayout(hbox, 1, 0, 1, 1)
# 窗体为QMainWindow需添加QWidget,在QWidget的setLayout设置布局控件
widget = QWidget()
widget.setLayout(maingridlayout)
self.setCentralWidget(widget)
# 窗体为QWidget则直接使用setLayout设置布局控件
# self.setLayout(vbox)
self.setGeometry(300, 300, 600, 400)
self.setWindowTitle('主窗体')
self.center()
# 子窗体必须先定义,否则闪退,即使采用exec_()不闪退,但无法接收子窗体传来的信号
self.Frmgauss_fs = frmgauss_fs.Form()
#连接信号
self.Frmgauss_fs._signal.connect(self.getparameter)
def center(self):
screen = QDesktopWidget().screenGeometry() # 获取屏幕的大小
self.move((screen.width() - self.width()) // 2,
(screen.height() - self.height()) // 2)
def btnClose_Click(self):
self.close()
def btnOption_Click(self):
self.Frmgauss_fs.show()
def getparameter(self, parameter):
# parameter在子窗体的信号中定义的数据类型为元组 _signal = pyqtSignal(tuple)
# (a, rf, L, falseE, falseN)=projectparam
self.txtbox.setPlainText(str(parameter))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
3 子窗体
import sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QMainWindow, QDialog, QLineEdit
from PyQt5.QtCore import pyqtSignal
# Ui_frmgauss_fs.py为Ui转py,窗体Ui_Form
from Ui_frmgauss_fs import Ui_Form
# 子窗体包含几个QLineEdit和一个按钮,通过点击按钮,向主窗体发送参数(由几个QLineEdit数值组成元组)
class Form(QDialog, Ui_Form):
# 信号定义的数据类型为元组
_signal = pyqtSignal(tuple)
def __init__(self, *args, **kwargs):
super(Form, self).__init__(*args, **kwargs)
self.setupUi(self)
self.projectparam = (6378137.0, 298.257222101, 114.3333333333,
800000.0, 0.0)
# Ui_Form中定义的按钮事件slot
self.btnCAL.clicked.connect(self.slot)
def getlineEditText(self, txtbox):
v = float(
txtbox.text() if txtbox.text() != "" else txtbox.placeholderText())
return v
def slot(self):
a = float(self.getlineEditText(self.lineEdit_a))
rf = float(self.getlineEditText(self.lineEdit_rf))
L = float(self.getlineEditText(self.lineEdit_L))
falseE = float(self.getlineEditText(self.lineEdit_falseE))
falseN = float(self.getlineEditText(self.lineEdit_falseN))
self.projectparam = (a, rf, L, falseE, falseN)
# 信号发送
self._signal.emit(self.projectparam)
if __name__ == '__main__': # 程序的入口
app = QApplication(sys.argv)
Frmgauss_fs = Form()
Frmgauss_fs.show()
sys.exit(app.exec_())
jcstone 2021-5-15