Figuring out which menu item was triggered in Qt

qt

Solution

You have two options:

- Call `sender()` in the slot, which will return the action that triggered the signal.

- Use `QSignalMapper`.

Problem

In a Qt application, I have a bunch of automatically-created QActions (menu items) that I add to a menu in the menu bar. Each opens a different file. I'd like to connect them all to the same slot so as to not write the same code many times. From that slot, though, how do I figure out which of the QActions was triggered? (Example: In Cocoa I'd do this with the sender parameter in the action selector.) Thanks!

Original source