Slot gets called twice despite pyqtSlot decorator

2019-09-15 01:46发布

问题:

This is a class which form i made in qt5 designer. The slot is called twice when I click the button.

class CustomerList(QWidget, Ui_CustomerList):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)
        self.buttX.clicked.connect(self.on_buttX_clicked)

    @pyqtSlot()
    def on_buttX_clicked(self):
        print("on_buttX_clicked")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = CustomerList()
    w.show()

    sys.exit(app.exec_())

What am I missig here?

回答1:

Your button is called buttX in designer, so the "Auto connect by name" feature in setupUi() finds a matching slot.

You can either

  1. remove the explicit connect
  2. rename the button
  3. rename the slot

I would personally go for the latter, i.e. use a slot name that does not have the pattern the "auto name connect" is looking for. E.g. onButtXClicked