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. different behavior of pressed vs clicked with lambdas

different behavior of pressed vs clicked with lambdas

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 440 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.
  • V Offline
    V Offline
    VirtualFloat
    wrote on last edited by
    #1

    I'm using lambda functions to define the actions for dynamically generated buttons. To "freeze" the value of the loop variable "i" assign it to a local variable "n" in the way described here https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result

    This works fine for 'pressed',

    button.pressed.connect(lambda a=i : print(f'pressed {a}'))
    

    --> "pressed 7"

    but somehow fails for 'clicked'.

    button.clicked.connect(lambda a=i: print(f'clicked {a}'))
    

    --> "clicked False"

    Reproduce:

    from PySide2.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
    
    app = QApplication()
    
    window = QWidget()
    layout = QVBoxLayout()
    
    for i in range(10):
        button = QPushButton()
    
        if i > 4:
            button.setText(f'pressed {i}')
            button.pressed.connect(lambda a=i : print(f'pressed {a}'))
        else:
            button.setText(f'clicked {i}')
            button.clicked.connect(lambda a=i: print(f'clicked {a}'))
    
        layout.addWidget(button)
    
    window.setLayout(layout)
    
    window.show()
    
    app.exec_()
    
    

    I fail to understand why this happens. Is there a fundamental difference in the way that 'pressed' and 'clicked' are handled? Or is this a bug?

    eyllanescE 1 Reply Last reply
    0
    • V VirtualFloat

      I'm using lambda functions to define the actions for dynamically generated buttons. To "freeze" the value of the loop variable "i" assign it to a local variable "n" in the way described here https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result

      This works fine for 'pressed',

      button.pressed.connect(lambda a=i : print(f'pressed {a}'))
      

      --> "pressed 7"

      but somehow fails for 'clicked'.

      button.clicked.connect(lambda a=i: print(f'clicked {a}'))
      

      --> "clicked False"

      Reproduce:

      from PySide2.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
      
      app = QApplication()
      
      window = QWidget()
      layout = QVBoxLayout()
      
      for i in range(10):
          button = QPushButton()
      
          if i > 4:
              button.setText(f'pressed {i}')
              button.pressed.connect(lambda a=i : print(f'pressed {a}'))
          else:
              button.setText(f'clicked {i}')
              button.clicked.connect(lambda a=i: print(f'clicked {a}'))
      
          layout.addWidget(button)
      
      window.setLayout(layout)
      
      window.show()
      
      app.exec_()
      
      

      I fail to understand why this happens. Is there a fundamental difference in the way that 'pressed' and 'clicked' are handled? Or is this a bug?

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @VirtualFloat Change to:

      • button.clicked[bool].connect(lambda checked, a=i: print(f'clicked {a}'))

      OR

      • button.clicked.connect(lambda *args, a=i: print(f'clicked {a}'))

      See https://stackoverflow.com/a/65185119/6622587 for more information.

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      3
      • V Offline
        V Offline
        VirtualFloat
        wrote on last edited by
        #3

        Ah, so clicked gets an argument where pressed doesn't. Thanks!

        I like the button.clicked.connect(lambda *args, a=i: print(f'clicked {a}')) solution because it will work even if the signature would change.

        1 Reply Last reply
        0

        • Login

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