tags:

views:

902

answers:

2

I have a taskbar menu that when clicked is connected to a slot that gets the trigger event. Now the problem is that I want to know which menu item was clicked, but I don't know how to send that information to the function connected to. Here is the used to connect the action to the function:

QtCore.QObject.connect(menuAction, 'triggered()', menuClickedFunc)

I know that some events return a value, but triggered() doesn't. So how do I make this happen? Do I have to make my own signal?

+5  A: 

Use a lambda

Here's an example from the PyQt book:

self.connect(button3, SIGNAL("clicked()"),
    lambda who="Three": self.anyButton(who))

By the way, you can also use functools.partial, but I find the lambda method simpler and clearer.

Eli Bendersky
Hey, thanks so much! Totally saved me LOTS of frustration. I should probably get a hold of that book...
You really should - it's a very good book.
Eli Bendersky
A: 

In general, you should have each menu item connected to a different slot, and have each slot handle the functionality only for it's own menu item. For example, if you have menu items like "save", "close", "open", you ought to make a separate slot for each, not try to have a single slot with a case statement in it.

If you don't want to do it that way, you could use the QObject::sender() function to get a pointer to the sender (ie: the object that emitted the signal). I'd like to hear a bit more about what you're trying to accomplish, though.

KeyserSoze
The OP is asking about a perfectly legitimate need. Sometimes many menu items are auto-generated (or just very similar) and should refer to the same slot. There is a standard way to achieve this in PyQt which is recommended in the book and in tutorials.
Eli Bendersky