GitHub连接:
本专栏所有源代码的GitHub直通车
这种方法有点奇怪
不过学完这种方式之后,我感觉这种方式好像没有那么灵活对吧,这个等下会提一下
先看代码
主程序代码:
"""
/********************************************************************************
* @Filename: 装饰器信号与槽.py
* @Author: haomingHu
* @Version: 1.0
* @Date: 2020-12-16
* @Description:
* @History:
********************************************************************************/
"""
from ui4 import Ui_Form#导入在QTdesigner设计好的 ui.py 文件
import sys
from PyQt5 import QtWidgets,QtCore
class mydesigner(QtWidgets.QWidget,Ui_Form):
def __init__(self):
super(mydesigner,self).__init__()
self.setupUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
@QtCore.pyqtSlot()
def on_open_clicked(self):
self.calendarWidget.close()
@QtCore.pyqtSlot()
def on_close_clicked(self):
self.calendarWidget.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myshow = mydesigner()
myshow.show()
sys.exit(app.exec_())
ui文件代码:
"""
/********************************************************************************
* @Filename: ui4.py
* @Author: haomingHu
* @Version: 1.0
* @Date: 2020-12-16
* @Description:
* @History:
********************************************************************************/
"""
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui4.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(432, 325)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(40, 220, 75, 23))
self.pushButton.setObjectName("open")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(290, 220, 75, 23))
self.pushButton_2.setObjectName("close")
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.calendarWidget.setGeometry(QtCore.QRect(80, 10, 248, 197))
self.calendarWidget.setObjectName("calendarWidget")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
self.pushButton_2.setText(_translate("Form", "PushButton"))
其它的代码看看就行了,主要看下面这几行代码:
@QtCore.pyqtSlot()
def on_open_clicked(self):
self.calendarWidget.close()
@QtCore.pyqtSlot()
def on_close_clicked(self):
self.calendarWidget.show()
首先,用这种方法的前提就是必须要先执行下面这一行代码,(这行代码在setupUi函数里面有)
QtCore.QMetaObject.connectSlotsByName(Form)
这行代码是pyqt5中根据信号名称自动连接到槽函数的核心代码,这行代码用来将QObject中子孙对象的某些信号按照其objextName连接到相应的槽函数汇总,
其次连接的格式就是:
@QtCore.pyqtSlot()
def on_objectSignal_function(self):
objectName就是控件的名字
function就是要连接的控件的信号,在这里是点击信号 clicked
突发奇想的一个问题就是,如果我的objectName是一个中文名,那这个functionName岂不是很拗口!!
好吧,这个就讲到这里,至于这种方法怎么出啊参数,感觉也是跟emit一样,差不多的!