自定义信号:
1.装饰器信号
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'class_ts.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(537, 509) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem) self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem1) self.label = QtWidgets.QLabel(self.centralwidget) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit.setMinimumSize(QtCore.QSize(150, 30)) self.lineEdit.setMaximumSize(QtCore.QSize(150, 30)) self.lineEdit.setObjectName("lineEdit") self.horizontalLayout.addWidget(self.lineEdit) spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem2) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_2.addItem(spacerItem3) self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setObjectName("label_2") self.horizontalLayout_2.addWidget(self.label_2) self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit_2.setMinimumSize(QtCore.QSize(150, 30)) self.lineEdit_2.setMaximumSize(QtCore.QSize(150, 30)) self.lineEdit_2.setObjectName("lineEdit_2") self.horizontalLayout_2.addWidget(self.lineEdit_2) spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_2.addItem(spacerItem4) self.verticalLayout.addLayout(self.horizontalLayout_2) self.hm_1 = QtWidgets.QPushButton(self.centralwidget) self.hm_1.setObjectName("hm_1") self.verticalLayout.addWidget(self.hm_1) self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setMinimumSize(QtCore.QSize(200, 50)) self.pushButton.setMaximumSize(QtCore.QSize(200, 50)) self.pushButton.setStyleSheet("background-color: rgb(85, 170, 0);") self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton, 0, QtCore.Qt.AlignHCenter) spacerItem5 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem5) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 537, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "帐号")) self.label_2.setText(_translate("MainWindow", "密码")) self.hm_1.setText(_translate("MainWindow", "PushButton")) self.pushButton.setText(_translate("MainWindow", "登录"))
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# -*- coding:utf-8 -*- from PyQt5.Qt import * from class_ts import Ui_MainWindow class Window(QMainWindow,Ui_MainWindow): def __init__(self): super().__init__() self.setWindowTitle("装饰器连接信号") self.resize(500,500) self.setupUi(self) @pyqtSlot(bool) def on_hm_1_clicked(self,val): # on_hm_1_clicked中的hm_1是对应控件名 print("按纽被点击了",val) @pyqtSlot(bool) def on_pushButton_clicked(self,val): # on_pushButton_clicked中的pushButton是对应控件名 print("按纽被点击了",val) @pyqtSlot() def on_pushButton_pressed(self): # on_pushButton_pressed中的pushButton是对应控件名 print("按纽被按下了") if __name__=="__main__": import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
2.自定义信号传送数据
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# -*- coding:utf-8 -*- from PyQt5.Qt import * # 定义一个类,重写QPushButton鼠标点击事件 class Btn(QPushButton): # 自定义一个信号 # rightClicked = pyqtSignal(str) rightClicked = pyqtSignal([str],[int]) def mousePressEvent(self,evt): super().mousePressEvent(evt) print(evt) print(evt.button()) # QMouseEvent # 判断某个键点击了 if evt.button() == Qt.RichText: # RichText = 1 是左键, RightButton = 2 是右键 print("应该发射左击信号了") # self.rightClicked.emit(self.text()) self.rightClicked[str].emit(self.text()) self.rightClicked[int].emit(888) class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("ui学习") self.resize(500,500) # self.setupUi(self) self.setup_ui() def setup_ui(self): # 创建对象 btn = Btn("xxxx",self) # rightClicked是自定义的信号 # content接收数据 btn.rightClicked[int].connect(lambda content:print("按纽被点击了",content)) if __name__=="__main__": import sys app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
3.自定义信号
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
from PyQt5.QtWidgets import QApplication import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import QStandardItemModel, QStandardItem from PyQt5.QtCore import pyqtSignal class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle('') self.initUI() self.show() def initUI(self): table = MyTableView(self) model = QStandardItemModel() model.setColumnCount(3) [model.appendRow([QStandardItem(str(i + j)) for i in [0, 1, 2]]) for j in [1, 2, 3]] table.setModel(model) table.doubleClickedItem.connect(print) self.setCentralWidget(table) class MyTableView(QTableView): doubleClickedItem = pyqtSignal(str) def __init__(self, parent=None): super(MyTableView, self).__init__(parent) self.doubleClicked.connect(self.doubleClickedHandle) def doubleClickedHandle(self, index): text = self.model().item(index.row(), 0).text() # 单元格没有值时会出错,None type没有text()这个方法,如有需要自己catch self.doubleClickedItem.emit(text) if __name__ == '__main__': app = QApplication(sys.argv) mw = MainWindow() sys.exit(app.exec_())
重构退出事件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# 重构退出事件。否则子窗口不关闭 def closeEvent(self, event): sys.exit() sys.exit(app.exec_())
自定义对话框1
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# -*- coding: utf-8 -*-# import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from Dialog_Pane import DateDialog class WinForm(QWidget): def __init__(self,parent=None): super().__init__(parent) def __init__(self): super().__init__() self.resize(400,90) self.setWindowTitle('对话框关闭时返回值给主窗口的例子') self.lineEdit=QLineEdit(self) self.button1=QPushButton('弹出对话框1') self.button1.clicked.connect(self.Button1Clicked) gridLayout=QGridLayout(self) gridLayout.addWidget(self.lineEdit) gridLayout.addWidget(self.button1) def Button1Clicked( self ): dialog=DateDialog(self) result=dialog.exec_() date = dialog.datetime.text() print(date) self.lineEdit.setText(date) print('result=%s'%result)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# -*- coding: utf-8 -*-# from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * class DateDialog(QDialog): def __init__(self,parent=None): super().__init__(parent) self.setWindowTitle('DateDialog') #在布局中添加控件 layout=QVBoxLayout(self) self.datetime=QLineEdit(self) # self.datetime.setCalendarPopup(True) # self.datetime.setDateTime(QDateTime.currentDateTime()) layout.addWidget(self.datetime) buttons=QPushButton(self,text="XXXX") buttons.clicked.connect(self.close) layout.addWidget(buttons)
自定义对话框2
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QPushButton from PyQt5.QtCore import Qt, pyqtSignal class MainWindow(QMainWindow): """ 主窗口类 """ my_signal = pyqtSignal() def __init__(self, *args): super(MainWindow, self).__init__(*args) # 设置主窗口的标题及大小 self.setWindowTitle('主窗口') self.resize(400, 300) # 创建按钮 self.btn = QPushButton(self) self.btn.setText('弹出对话框') self.btn.clicked.connect(self.show_dialog) # 自定义信号绑定 self.my_signal.connect(self.test) # 创建对话框对象 self.dialog = Dialog(self) def show_dialog(self): self.dialog.show() self.dialog.exec() def test(self): self.btn.setText('我改变了') class Dialog(QDialog): """ 对话框类 """ my_signal = pyqtSignal() def __init__(self, parent, *args): super(Dialog, self).__init__(*args) # 设置对话框的标题及大小 self.setWindowTitle('对话框') self.resize(200, 200) self.setWindowModality(Qt.ApplicationModal) self.btn = QPushButton(self) self.btn.setText('改变主窗口按钮的名称') self.btn.clicked.connect(self.my_signal.emit) # 自定义信号绑定 self.my_signal.connect(parent.my_signal.emit) if __name__ == '__main__': app = QApplication(sys.argv) demo = MainWindow() demo.show() sys.exit(app.exec())