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

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

      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.

        1. menubar.py
          0.6 kB
        2. pyside2356.py
          0.9 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

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

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes