QtCore connectSlotsByName, 无其他connect函数,但是仍然每次被激活2次

#!/usr/bin/env python
# coding: utf-8

import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QWidget
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(432, 243)
        self.gridLayoutWidget = QtWidgets.QWidget(Form)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 60, 411, 171))
        self.pushButton = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.addWidget(self.pushButton, 4, 0, 1, 1)

        self.retranslateUi(Form)

        QtCore.QMetaObject.connectSlotsByName(Form)

        #self.pushButton.clicked.connect(MyForm.showit)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "Add Slots"))
class MyForm(QMainWindow,Ui_Form):
    def __init__(self):
        super(MyForm, self).__init__()
        self.setupUi(self)

采用connectSlotsByName,用cliecked命名,如下就总是运行2次

    #@QtCore.pyqtSignature("")
    def on_pushButton_clicked(self):
        print ("A")
    @staticmethod
    def showit(self):
        print ("B")
app = QApplication(sys.argv)
a = MyForm()
a.show()
app.exec()
讨论数量: 2

Thanks. yes. I found the solution. from PyQt5.QtCore import pyqtSlot @pyqtSlot() def on_pushButton_clicked(self): but frankly, I didn't real understand the purpose the pyqtSlot decorator. Anyway, I still study it and Thank you very much.

When you use the decorator @pyqtSlot you are indicating the signature (ie the type of arguments), so the connect method will only make the connection with the signal that matches the slot signature, so you do not see the problem anymore since you use the signal no arguments

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!