PDA

View Full Version : Accessing QAction from within Slot



stefanadelbert
25th February 2010, 02:59
I'm dynamically creating menu items (using QMenu.AddAction) and connecting each QAction.triggered() to the one slot. Inside the slot function I need to know which QAction triggered the slot and ideally be able to get the data (QAction.data()) from that QAction.

Is there a way to get hold of the QAction or is there another/better way to achieve this?

Many thanks
Stefan

stefanadelbert
25th February 2010, 03:03
Found the answer:


When inserting action items you usually specify a receiver and a slot. The receiver will be notifed whenever the item is triggered(). In addition, QMenu provides two signals, activated() and highlighted(), which signal the QAction that was triggered from the menu.
From QMenu Class Reference

stefanadelbert
25th February 2010, 03:06
It keeps on getting better:


void QMenu::triggered ( QAction * action ) [signal]
This signal is emitted when an action in this menu is triggered.

action is the action that caused the signal to be emitted.

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".

Note: This signal is emitted for the main parent menu in a hierarchy. Hence, only the parent menu needs to be connected to a slot; sub-menus need not be connected.

See also hovered() and QAction::triggered().

From QMenu Class Reference