Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2524

Lambda that captures extra parameters cannot be connected to signal that has default argument values

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4: Low
    • 6.7.0
    • 6.6.0
    • PySide
    • None
    • 9f7b808f6 (dev), ef38449d2 (dev), 681611449 (dev), 213684902 (6.6), 20d04ea0c (6.6), 782c0757f (dev), e79aea5bd (dev), daf77773a (dev), 9a0099a23 (6.6), 51636b43d (dev)

    Description

      Consider the lambda in this loop. It captures the extra parameter b so that setText is called on the correct widget:

      for i in range(3):
          button = QCheckBox("Click me")
          button.clicked.connect(
              lambda checked, b=button: b.setText("Checked!" if checked else "Unchecked!"))
      

      As of PySide6 6.6.0, clicking a checkbox in this example raises:

      TypeError: <lambda>() missing 1 required positional argument: 'checked'

      The signal QAbstractButton::clicked has one argument checked which has a default value.

      The issue does not occur when connecting a lambda to a signal that has no default values for its arguments. For example, connecting a similar lambda to QCheckBox::stateChanged works fine:

      button.stateChanged.connect(lambda state, b=button: b.setText(f"New state: {state}"))
      

      Note: The attached example file produces the expected results under your competitor's implementation.

      Attachments

        1. pyside2524_diag.diff
          9 kB
        2. pyside2524_log.txt
          2 kB
        3. pyside2524.py
          2 kB
        4. PYSIDE-2524.py
          1 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kleint Friedemann Kleint
            ij Iliyas Jorio
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: