PDA

View Full Version : "anonymous" QActions



tuli
3rd May 2013, 17:52
I want to have a variable amount of INTs in a menu, and if the user clicks on one of the items, the QAction triggers() and i process the integer that belongs to the menu item.

The problem is that the number of INTs to be displayed in the menu varies, and that i actually always call the same function for processing the INT number. A static amount of QActions and a slot for each actions is therefore inefficient.

It's not a perfect solution, but the window owning the menu could keep a vector of actions. If the context menu event fires, the vector is emptied and refilled with the desired amount of QActions.
This would manage the lifetime of the QActions but does not address the core problem: connecting to the triggered() event of an QAction, there is no way of knowing WHICH Action was fired, and thus which number to process.

Is there any good way to work around this?

d_stranz
3rd May 2013, 18:20
Would QSignalMapper help?

anda_skoa
3rd May 2013, 22:17
Another option is to set the the reference value (in your case the INT), as the action's data and then retrieve the action in the slot using QObject::sender().

However, I would also suggest QSignalMapper since it was specifically designed to map from a parameter-less signal to a single parameter slot (in your case an int parameter).

Cheers,
_