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

Functor not working as target of connect in a loop

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • Not Evaluated
    • None
    • 6.5.1
    • PySide
    • None
    • Pyside 6.5.1
    • Linux/X11

    Description

      Using a functor class as a target for connect in a loop

      from sys import argv
      from sys import exit
      
      from PySide6.QtGui import QAction
      from PySide6.QtWidgets import QApplication
      from PySide6.QtWidgets import QMainWindow
      
      class PrintNum:
        def __init__(self, n):
          self.n = n
      
        def __call__(self):
          print(self.n)
      
      class MenuBar(QMainWindow):
        def __init__(self):
          super().__init__()
          menu = self.menuBar().addMenu("Num")
          for i in range(5):
            action = QAction(f"{i}", self)
            action.triggered.connect(PrintNum(i))
            menu.addAction(action)
      
      app = QApplication(argv)
      menuBar = MenuBar()
      menuBar.show()
      exit(app.exec())
      

      the first one will always be used. In this case for any selection always 0 is printed.

      This worked in former Pyside6 versions and also works with PyQt.

      Attachments

        1. pyside2356.py
          0.9 kB
        2. menubar.py
          0.6 kB

        Issue Links

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

          Activity

            People

              crmaurei Cristian Maureira-Fredes
              wolf.seifert Wolfgang Seifert
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes