Search code examples
c++qtqt-designerqactionqmenubar

How to add action to QAction when clicked


I just created a simple app using Qt and Qt Designer and added a QMenuBar with a menu.

How can I make it so certain code will be executed if a QAction Element is triggered?

MenuBar with QActions


Solution

  • You can use Qt Designer's Action Editor, where you can find all the actions you added to your form, and edit them.

    In you case, you can do the following in your action editor:

    • Right click on an action
    • Click Go to slot
    • Choose triggered()
    • Qt Creator will add the slot declaration in your header, and will direct you to its definition so you can add the code you want executed when the action's widget is clicked.

    Here's an example:

    Action editor in Qt Desginer