Menu

Can you get the sender from a button clicked() event?

Help
2015-12-01
2016-02-26
  • Brad Elliott

    Brad Elliott - 2015-12-01

    I'm curious as to whether there is a way to access the sender from a button click event. In C++ I've used the static method QObject::sender() to get the sender of the event but I can't seem to figure out how to do this in Python.

    def my_click_handler():
        # can I get access to the sender here?
        ...
    
    btn = qt.QPushButton(label, parent)
    btn.connect('clicked()', my_click_handler)
    

    By the way, thanks for all the hard work you've done on PythonQt. It is a fabulous project.

     
  • Brad Elliott

    Brad Elliott - 2015-12-01

    Oh, I think I've figured out a way to do what I wanted. I can just use a closure. The inner function has access to objects in the parent scope so I can access the sender of the event that way.

        def dummy_button_test(parent):
            label = "ClickMe"
            btn = qt.QPushButton(label, parent)
    
            # Create an inner function to handle the button click. It has
            # access to everything in the scope of dummy_button_test.
            def my_click_handler():
                print btn
    
            btn.connect('clicked()', my_click_handler)
    
     
    • Alan Ezust

      Alan Ezust - 2015-12-23

      QObject.sender() is not a static method, and you can call it from python.
      However, depending on the connection type, sender() might not be set
      properly. I noticed if I make the connectionType Qt.QueuedConnection it
      works when sometimes Qt.DirectConnection does not.

      On Tue, Dec 1, 2015 at 6:39 AM, Brad Elliott bradfordelliott@users.sf.net
      wrote:

      Oh, I think I've figured out a way to do what I wanted. I can just use a
      closure. The inner function has access to objects in the parent scope so I
      can access the sender of the event that way.

      ~~~~
      def dummy_button_test(parent):
      label = "ClickMe"
      btn = qt.QPushButton(label, parent)

          # Create an inner function to handle the button click. It has
          # access to everything in the scope of dummy_button_test.
          def my_click_handler():
              print btn
      
          btn.connect('clicked()', my_click_handler)
      

      ~~~~


      Can you get the sender from a button clicked() event?


      Sent from sourceforge.net because you indicated interest in <
      https://sourceforge.net/p/pythonqt/discussion/631393/>

      To unsubscribe from further messages, please visit <
      https://sourceforge.net/auth/subscriptions/>

       
  • Devin

    Devin - 2016-02-26

    You can also use lambda functions, as described here:
    http://stackoverflow.com/questions/27262288/qt-connect-slot-with-argument-using-lambda

    def my_click_handler(checked, sender):
        print sender.name()
    
    btn = qt.QPushButton(label, parent)
    btn.connect('clicked()', lambda c, p=btn: my_click_handler(c,p))
    

    Makes it quite slick!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.