使用pyqt的时候需要在子窗体和主窗体之间进行数据的传输。
新建子窗体的名字为DateDialog2.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DateDialog(QDialog):
Signal_OneParameter = pyqtSignal(str)
def __init__(self,parent =None):
super(DateDialog,self).__init__(parent)
self.setWindowTitle('子窗口:用来发射信号')
#在布局中添加控件
layout = QVBoxLayout(self)
self.label = QLabel(self)
self.label.setText('前者发射内置信号\n后者发射自定义信号')
self.datetime_inner = QDateTimeEdit(self)
self.datetime_inner.setCalendarPopup(True)
self.datetime_inner.setDateTime(QDateTime.currentDateTime())
self.datetime_emit = QDateTimeEdit(self)
self.datetime_emit.setCalendarPopup(True)
self.datetime_emit.setDateTime(QDateTime.currentDateTime())
layout.addWidget(self.label)
layout.addWidget(self.datetime_inner)
layout.addWidget(self.datetime_emit)
#使用两个按钮分别连接accept()和reject()槽函数
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal,self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
self.datetime_emit.dateTimeChanged.connect(self.emit_signal)
layout.addWidget(buttons)
def emit_signal(self):
date_str = self.datetime_emit.dateTime().toString()
self.Signal_OneParameter.emit(date_str)
主窗体的名字为CallDialogMainWin2.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from DateDialog2 import DateDialog
class WinForm(QWidget):
def __init__(self, parent =None):
super(WinForm,self).__init__(parent)
self.resize(400,90)
self.setWindowTitle('信号与槽传递参数的示例')
self.open_btn = QPushButton('获取时间')
self.lineEdit_inner = QLineEdit(self)
self.lineEdit_emit = QLineEdit(self)
self.open_btn.clicked.connect(self.openDialog)
self.lineEdit_inner.setText('接受子窗口内置信号的时间')
self.lineEdit_emit.setText('接受子窗口自定义信号的时间')
grid = QGridLayout()
grid.addWidget(self.lineEdit_inner)
grid.addWidget(self.lineEdit_emit)
grid.addWidget(self.open_btn)
self.setLayout(grid)
def openDialog(self):
dialog = DateDialog(self)
'''连接子窗口的内置信号与主窗口的槽函数'''
dialog.datetime_inner.dateTimeChanged.connect(self.deal_inner_slot)
'''连接子窗口的自定义信号与主窗口的槽函数'''
dialog.Signal_OneParameter.connect(self.deal_emit_slot)
dialog.show()
def deal_inner_slot(self,date):
self.lineEdit_inner.setText(date.toString())
def deal_emit_slot(self,dateStr):
self.lineEdit_emit.setText(dateStr)
if __name__=='__main__':
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())
运行效果
当修改子窗体上的日期,会自动自动传递给主窗体
self.datetime_emit.dateTimeChanged.connect(self.emit_signal)
def emit_signal(self):
date_str = self.datetime_emit.dateTime().toString()
self.Signal_OneParameter.emit(date_str)
关键是如何实现实现符合特定条件的自定义信号的发射问题?
当控件datetime_emit的时间发生变化时,就会触发子窗口的槽函数emit_signal,而在这个槽函数中又会发射自定义信号Signal——OneParameter,这个信号函数为了传递date_str参数给主函数的槽函数。
def openDialog(self):
dialog = DateDialog(self)
'''连接子窗口的内置信号与主窗口的槽函数'''
dialog.datetime_inner.dateTimeChanged.connect(self.deal_inner_slot)
'''连接子窗口的自定义信号与主窗口的槽函数'''
dialog.Signal_OneParameter.connect(self.deal_emit_slot)
dialog.show()
主窗体的关键是将子窗口的内置信号与自定义信号绑定到主窗口的槽函数。
###参考pyqt5快速开发与实践