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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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.
# 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)
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.
By the way, thanks for all the hard work you've done on PythonQt. It is a fabulous project.
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.
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:
You can also use lambda functions, as described here:
http://stackoverflow.com/questions/27262288/qt-connect-slot-with-argument-using-lambda
Makes it quite slick!