# -*- coding:utf-8 -*-
"""
# @Time:2022/12/13 0013 0:00
# @Author:晚秋拾叶
# @File:qt07_winSignalSlot06.py
# @PyCharm之Python
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class WinForm(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lcd = QLCDNumber(self)
slider = QSlider(Qt.Horizontal, self)
vBox = QVBoxLayout()
vBox.addWidget(lcd)
vBox.addWidget(slider)
self.setLayout(vBox)
# 此处是内置信号与内置槽的基本应用了
slider.valueChanged.connect(lcd.display)
self.setGeometry(300, 300, 550, 300)
self.setWindowTitle("信号与槽:连接滑块和LCD")
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
app.exec_()
这个程序首先创建滑块和LCD控件,而且这两个控件都是系统自带的,很容易实现。
lcd = QLCDNumber(self)
slider = QSlider(Qt.Horizontal, self)
然后,通过垂直布局器完成布局,而且还要放到QWidget总控件中。
vBox = QVBoxLayout()
vBox.addWidget(lcd)
vBox.addWidget(slider)
self.setLayout(vBox)
最后,接连到QSlidre控件的valueChanged()信号函数和LCD面板控件的display()槽函数。这样就是一个典型的内置信号与内置槽的接连了。
除了valueChange()之外,QSlider还有sliderPressured()、sliderMoved()、sliderReleased()等信号函数。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DateDialog(QDialog):
def __init__(self, parent=None):
super(DateDialog, self).__init__(parent)
self.setWindowTitle('DateDialog')
# 在布局中添加部件
layout = QVBoxLayout(self)
self.datetime = QDateTimeEdit(self)
self.datetime.setCalendarPopup(True)
self.datetime.setDateTime(QDateTime.currentDateTime())
layout.addWidget(self.datetime)
# 使用两个button(ok和cancel)分别连接accept()和reject()槽函数
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
# 从对话框中获取当前日期和时间
def dateTime(self):
return self.datetime.dateTime()
# 静态方法创建对话框并返回 (date, time, accepted)
@staticmethod
def getDateTime(parent=None):
dialog = DateDialog(parent)
result = dialog.exec_()
date = dialog.dateTime()
return (date.date(), date.time(), result == QDialog.Accepted)
# -*- coding:utf-8 -*-
"""
# @Time:2022/12/13 0013 0:23
# @Author:晚秋拾叶
# @File:qt07_callDialogMainWin.py
# @PyCharm之Python
"""
import sys
from PyQt5.QtWidgets import *
from qt07_DateDialog import DateDialog
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.resize(400, 90)
self.setWindowTitle('对话框关闭时返回值给主窗口例子')
self.lineEdit = QLineEdit(self)
self.button1 = QPushButton('弹出对话框1')
self.button1.clicked.connect(self.onButton1Click)
self.button2 = QPushButton('弹出对话框2')
self.button2.clicked.connect(self.onButton2Click)
gridLayout = QGridLayout()
gridLayout.addWidget(self.lineEdit)
gridLayout.addWidget(self.button1)
gridLayout.addWidget(self.button2)
self.setLayout(gridLayout)
def onButton1Click(self):
dialog = DateDialog(self)
result = dialog.exec_()
date = dialog.dateTime()
self.lineEdit.setText(date.date().toString())
print('\n日期对话框的返回值')
print('date=%s' % str(date.date()))
print('time=%s' % str(date.time()))
print('result=%s' % result)
dialog.destroy()
def onButton2Click(self):
date, time, result = DateDialog.getDateTime()
self.lineEdit.setText(date.toString())
print('\n日期对话框的返回值')
print('date=%s' % str(date))
print('time=%s' % str(time))
print('result=%s' % result)
if result == QDialog.Accepted:
print('点击确认按钮')
else:
print('点击取消按钮')
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())
# -*- coding:utf-8 -*-
"""
# @Time:2022/12/13 0013 1:03
# @Author:晚秋拾叶
# @File:qt07_DateDialog2.py
# @PyCharm之Python
"""
from PyQt5.QtCore 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)
# 使用两个button(ok和cancel)分别连接accept()和reject()槽函数
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
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)
# -*- coding:utf-8 -*-
"""
# @Time:2022/12/13 0013 1:04
# @Author:晚秋拾叶
# @File:qt07_callDialogMainWin2.py
# @PyCharm之Python
"""
import sys
from PyQt5.QtWidgets import *
from qt07_DateDialog2 import DateDialog
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.resize(550, 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_())