I am attempting to reduce the amount of signals I have to use in my contextmenus. The menu consists of actions which switches the operation mode of the program, so the operation carried out by the slots is very simple. Quoting the documentation on QMenu::triggered,
Normally, you connect each menu action's triggered() signal to its own custom slot, but sometimes you will want to connect several actions to a single slot, for example, when you have a group of closely related actions, such as "left justify", "center", "right justify".
However, I can't figure out how to accomplish this, and the documentation does not go into any further detail.
Suppose I have actions actionOpMode1
and actionOpMode2
in menu actionMenu
, and a slot setOpMode
. I want setOpMode
to be called with a parameter which somehow relates to which of the actions was triggered. I tried various permutations on this theme:
QObject.connect(self.actionMenu, SIGNAL('triggered(QAction)'), self.setOpMode)
But I never even got it to call setOpMode, which suggests that actionMenu never "feels triggered", so to speak.
In this SO question, it's suggested that it can be done with lamdbas, but this:
QObject.connect(self.actionOpMode1, SIGNAL('triggered()'), lambda t: self.setOpMode(t))
gives "<lambda> () takes exactly 1 argument (0 given)"
. I can't say I really understand how this is supposed to work, so I may have done something wrong when moving from clicked() to triggered().
How is it done?