其他分享
首页 > 其他分享> > PyQt5基础学习-信号和槽的自动连接 1.@QtCore.pyqtSlot(定义为槽函数) 2.QpushButton().setObjectName("设置需要绑定的函数名")

PyQt5基础学习-信号和槽的自动连接 1.@QtCore.pyqtSlot(定义为槽函数) 2.QpushButton().setObjectName("设置需要绑定的函数名")

作者:互联网

@QtCore.pyqtSlot 定义为需要自动连接的槽函数, on_对象名_信号名字

AutoSignalSlot.py 

"""
信号与槽自动连接

on_对象名_signalname

"""
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
import sys

class AutoSignalSlot(QWidget):
    def __init__(self):
        super(AutoSignalSlot, self).__init__()
        self.okButton = QPushButton('ok', self)
        self.okButton.setObjectName("okButton")

        self.okButton1 = QPushButton('cancel', self)
        self.okButton1.setObjectName("cancelButton")


        layout = QHBoxLayout()
        layout.addWidget(self.okButton)
        self.setLayout(layout)
        QtCore.QMetaObject.connectSlotsByName(self)
        #self.okButton.clicked.connect(self.on_okButton_clicked)

    @QtCore.pyqtSlot()
    def on_okButton_clicked(self):
        print("点击了ok按钮")

    @QtCore.pyqtSlot()
    def on_cancelButton_clicked(self):
        print("点击了cancel按钮")

if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = AutoSignalSlot()
    main.show()

    sys.exit(app.exec_())

 

标签:__,setObjectName,clicked,函数,okButton,pyqtSlot,self,QpushButton,QtCore
来源: https://www.cnblogs.com/hyq-lst/p/15877199.html