多窗口传递数据 不使用信号及槽
D116.PY
-------------------------------------------------------------------------------------
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from D11602 import Datedialog
class mwin(QWidget):
def __init__(self, parent=None):
super(mwin, self).__init__(parent) # 为什么 PARENT?
lay = QGridLayout(self)
self.setWindowTitle("不使用信号和槽")
self.lineEdit = QLineEdit(self)
self.b1 = QPushButton("B1")
self.b2 = QPushButton("B2")
lay.addWidget(self.lineEdit)
lay.addWidget(self.b1)
lay.addWidget(self.b2)
self.setLayout(lay)
self.b1.clicked.connect(self.onb1click)
self.b2.clicked.connect(self.onb2click)
def onb1click(self):
print("1123")
dialog = Datedialog(self)
lres = dialog.exec()
Ldate = dialog.Fdatetime()
if lres == QDialog.Accepted:
print('点击确定按钮')
self.lineEdit.setText(Ldate.toString())
else:
print('点击取消按钮')
dialog.destroy()
def onb2click(self):
print("1234")
ldate,ltime,lres = Datedialog.getdatetime()
if lres == QDialog.Accepted:
print('点击确定按钮')
self.lineEdit.setText(ldate.toString())
else:
print('点击取消按钮')
# print(type(res1),"a",res1)
if __name__ =='__main__':
qa = QApplication(sys.argv)
mw = mwin()
mw.show()
sys.exit(qa.exec_())
-----------------------------------------------------------------
D11602.py
------------------------------------------------------------------
class Datedialog(QDialog):
def __init__(self,parent = None):
super(Datedialog,self).__init__(parent)
self.setWindowTitle("123")
self.datetime = QDateTimeEdit(self)
self.datetime.setCalendarPopup(True)
self.datetime.setDateTime(QDateTime.currentDateTime())
lay = QHBoxLayout(self)
lay.addWidget(self.datetime)
btns = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
lay.addWidget(btns)
btns.accepted.connect(self.accept)
btns.rejected.connect(self.reject)
def rab(self):
return (res1)
def Fdatetime(self):
return self.datetime.dateTime()
@staticmethod
def getdatetime(parent = None):
dialog = Datedialog(parent) # 为什么?
res = dialog.exec()
udate = dialog.Fdatetime()
return (udate.date(), udate.time(),res== QDialog.Accepted)
if __name__ =='__main__':
qa = QApplication(sys.argv)
mw = Datedialog()
mw.show()
sys.exit(qa.exec_())
多窗口传递数据 使用信号及槽
D117.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from D11702 import Datedialog
class mwin(QWidget):
sigal_c = pyqtSignal(str)
def __init__(self, parent=None):
super(mwin, self).__init__(parent)
lay = QGridLayout(self)
self.resize(400,120)
self.setWindowTitle("使用信号和槽")
self.lineEdit_inner = QLineEdit(self)
self.lineEdit_inner.setText("内置信号")
self.lineEdit_emit = QLineEdit(self)
self.lineEdit_emit.setText("自定义信号")
self.b1 = QPushButton("获取内置信号时间")
self.b2 = QPushButton("获取自定义信号时间")
lay.addWidget(self.lineEdit_inner)
lay.addWidget(self.lineEdit_emit)
lay.addWidget(self.b1)
lay.addWidget(self.b2)
self.setLayout(lay)
self.b1.clicked.connect(self.onb1click)
self.b2.clicked.connect(self.onb2click)
def onb1click(self):
print("1123")
dialog = Datedialog(self)
dialog.datetime_innner.dateTimeChanged.connect(self.inner_solt)
dialog.show()
def inner_solt(self,date):
self.lineEdit_inner.setText(date.toString())
def onb2click(self):
print("1234 ")
dialog = Datedialog(self)
dialog.sigal_1.connect(self.emit_solt)
dialog.show()
def emit_solt(self,datestr):
self.lineEdit_emit.setText(datestr)
# print(type(res1),"a",res1)
if __name__ =='__main__':
qa = QApplication(sys.argv)
mw = mwin()
mw.show()
sys.exit(qa.exec_())
-------------------------------------------
D11702.PY
------------------------------------------------
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Datedialog(QDialog):
sigal_1 = pyqtSignal(str)
def __init__(self,parent = None):
super(Datedialog,self).__init__(parent)
self.setWindowTitle("使用信号与槽进行数据传递")
self.datetime_innner = QDateTimeEdit(self)
self.datetime_innner.setCalendarPopup(True)
self.datetime_emit = QDateTimeEdit(self)
self.datetime_emit.setCalendarPopup(True)
self.datetime_innner.setDateTime(QDateTime.currentDateTime())
self.datetime_emit.setDateTime(QDateTime.currentDateTime())
lay = QGridLayout(self)
lay.addWidget(self.datetime_innner)
lay.addWidget(self.datetime_emit)
btns = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
lay.addWidget(btns)
btns.accepted.connect(self.accept)
btns.rejected.connect(self.reject)
self.datetime_emit.dateTimeChanged.connect(self.emit_solt1)
def Fdatetime(self):
return self.datetime.dateTime()
@staticmethod
def getdatetime(parent = None):
dialog = Datedialog(parent)
res = dialog.exec()
udate = dialog.Fdatetime()
return (udate.date(), udate.time(),res== QDialog.Accepted)
def emit_solt1(self):
date_str = self.datetime_emit.dateTime().toString()
self.sigal_1.emit(date_str)
if __name__ =='__main__':
qa = QApplication(sys.argv)
mw = Datedialog()
mw.show()
sys.exit(qa.exec_())
---------------------------------------------------------
总结:
定义信号 通过emit 传递参数给槽 触发槽