PYQT5 - 插槽函数不采用默认参数

PYQT5 - Slot function does not take default argument

我正在使用 PYQT5 接口,QPushButton 应该调用槽函数,它有默认参数。

self.button = QtWidgets.QPushButton("Button")
self.button.clicked.connect(self.doSomething)

def doSomething(self, boolVariable = True):
  print(boolVariable)

当我 运行 doSomething 函数时的结果:

[in] object_instance.doSomething()
--> True

但是如果我点击按钮,我得到这个结果:

--> False

谁能解释一下为什么不考虑默认参数?

谢谢!

QPushButton 的 clicked 信号,与继承自 QAbstractButton 的任何 class 一样,有一个 checked 参数,表示当前已检查 ("pressed") 的状态按钮。

This signal is emitted when the button is activated (i.e., pressed down then released while the mouse cursor is inside the button)

按钮在松开时发出点击信号;那时,按钮未被按下,信号参数将为 False.

有两种可能性可以避免这种情况:

  1. 将信号连接到 lambda:

    self.button.clicked.connect(lambda: self.doSomething())

  2. 向函数添加一个 pyqtSlot 装饰器,没有签名作为参数:

    @QtCore.pyqtSlot()
    def doSomething(self, boolVariable = True):
        print(boolVariable)