Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. connect() in for loop python 3.11
Forum Updated to NodeBB v4.3 + New Features

connect() in for loop python 3.11

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 793 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Captain Haddock
    wrote on 8 May 2023, 20:42 last edited by
    #1

    beginner here!

    In my for loop I connect buttons to a common method using a lambda to pass a loop index .. when I click both buttons I see "1" printed; I expected to see "0" and "1" .. how do I change the code to get the expected results?

    import sys
    from PyQt6 import QtWidgets
    
    
    class Foo(QtWidgets.QWidget):
    
        def __init__(self):
            super().__init__()
            self.layout = QtWidgets.QHBoxLayout()
            self.setLayout(self.layout)
    
        def setupUI(self):
            for index in range(2):
                button = QtWidgets.QPushButton(str(index))
                button.clicked.connect(lambda: self.buttonClicked(index))
                self.layout.addWidget(button)
    
        def buttonClicked(self, index):
            print(index)
    
    app = QtWidgets.QApplication(sys.argv)
    foo = Foo()
    foo.setupUI()
    foo.show()
    sys.exit(app.exec())
    
    C 1 Reply Last reply 8 May 2023, 21:54
    0
    • C Captain Haddock
      8 May 2023, 20:42

      beginner here!

      In my for loop I connect buttons to a common method using a lambda to pass a loop index .. when I click both buttons I see "1" printed; I expected to see "0" and "1" .. how do I change the code to get the expected results?

      import sys
      from PyQt6 import QtWidgets
      
      
      class Foo(QtWidgets.QWidget):
      
          def __init__(self):
              super().__init__()
              self.layout = QtWidgets.QHBoxLayout()
              self.setLayout(self.layout)
      
          def setupUI(self):
              for index in range(2):
                  button = QtWidgets.QPushButton(str(index))
                  button.clicked.connect(lambda: self.buttonClicked(index))
                  self.layout.addWidget(button)
      
          def buttonClicked(self, index):
              print(index)
      
      app = QtWidgets.QApplication(sys.argv)
      foo = Foo()
      foo.setupUI()
      foo.show()
      sys.exit(app.exec())
      
      C Offline
      C Offline
      Captain Haddock
      wrote on 8 May 2023, 21:54 last edited by
      #2

      @Captain-Haddock I found the answer here:
      https://stackoverflow.com/questions/4578861/connecting-slots-and-signals-in-pyqt4-in-a-loop

      1 Reply Last reply
      0
      • C Captain Haddock has marked this topic as solved on 8 May 2023, 21:54

      1/2

      8 May 2023, 20:42

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved