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

Connecting signal to inner function reuses first instance of inner function

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P1: Critical
    • 6.5.2, 6.6.0, 6.5.1.1
    • 6.5.1
    • PySide
    • None
    • Python 3.11
    • Linux/X11
    • 0bf8ce702 (dev), 80da3a11d (6.5.1), 9a7dfdd57 (tqtc/6.5.1), f21ed0581 (6.5)

    Description

      This is a regression in PySide6 6.5.1; the issue does not occur in 6.5.0.

      In 6.5.1, if you connect signals to separate instances of an inner function, the connection will actually use the first instance of that inner function.

      In other words, all connections to an inner function will reuse the variables that were captured the first time we instantiated the inner function. This leads to incorrect behavior when the signal is emitted, because the function acts on the wrong set of variables.

      In the snippet below, clicking a button is supposed to change the text in the same button.

      • In 6.5.0, this works fine;
      • In 6.5.1, Button2 and Button3 change Button1's text instead of their own.
      def connectPressed(button: QPushButton, text: str):
          def innerFunc():
              button.setText(text)
          button.pressed.connect(innerFunc)
      
      button1 = QPushButton("Button1")
      button2 = QPushButton("Button2")
      button3 = QPushButton("Button3")
      
      connectPressed(button1, "You clicked Button1")
      connectPressed(button2, "You clicked Button2")
      connectPressed(button3, "You clicked Button3")
      

      Note that the same behavior can be observed if connectPressed uses a lambda:

      def connectPressed(button: QPushButton, text: str):
          button.pressed.connect(lambda: button.setText(text))
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              adherrma Adrian Herrmann
              ij Iliyas Jorio
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes