Nominate our 2022 Qt Champions!

Connecting menu actions

  • Hi. I have a simple problem of creating and connecting functions to be attached to menu items.

    I found useful the Menus example bundled with QT Creator, but when using the Design tool it's a bit different. In ui_mainwindow.h it autocreates a class Ui_MainWindow while the example puts everything in the MainWindow class. So how would I connect the actions to the slots properly when using the Designer? If I define a private slot function in MainWindow class (mainwindow.h), it does not show up in the Signals & Slots editor.

  • Moderators

    Usually classes are defined this way (Qt Creator does that automatically for you):
    // Recalling from memory... reality might be a tad different :)
    MainWindow : public QMainWindow, public Ui::MainWindow

  • Hello.
    The easiest way of connecting slots with signals of actions in menu:

    Create new action in designer.

    Add it into menu/toolbar/etc.

    Right click on it and choose "Go to slot"

    Signal and slot will be auto connected.

  • [quote author="Wilk" date="1337774662"]Hello.
    The easiest way of connecting slots with signals of actions in menu:

    Create new action in designer.

    Add it into menu/toolbar/etc.

    Right click on it and choose "Go to slot"

    Signal and slot will be auto connected.[/quote]

    This did not work!

    if you right click on a menu item you only see
    insert devider
    delete action
    ( may be different strings because I use the german language)

    Also the use of F4 (edit Signal/Slots) is not usefull because in this way you only can select the menu itself and not the menu items

    The only thing that worked (for me) is using the action editor.
    selecting the action there and using the right button will allow to "display slot" selecting one and the editor open the according cpp file with a prototyp of the slot.

  • I have a project where I used a ui_mainwindow.h.

    in order to connect the action you need to use
    @connect( ui -> some_action, SIGNAL(triggered()), qApp, SLOT(some slot))@

  • @dexli
    The problem you are having is because you are clicking on the menu or toolbar items themselves. You have to click on the object created in the 'Actions' pane, usually on the side or bottom tool views. If you don't see this, right click on a toolbar area that is empty until you get the context menu that has a list of checkable items to view (or select Window->Views from the main menu). Ensure the 'Action Editor' is checked. You may have to uncheck it and recheck it to make it show up if it is hidden AND checked.

    To map the action's signals to a new slot functions automagically: Right click on any action and select 'go to slot' and double click 'triggered()' to create a function for it.

    To map signals directly to another object's existing slot: Ensure the "Signals and Slots Editor" toolwindow is visible using the same method mentioned above. Map the action to the signal, for example, from left to right (in the s&s editor), mapping the "Exit" menu item/action to the "close()" function of QMainWindow is simple as doing this: in the drop down boxes, left to right, pick "actionExit" under sender, then 'triggered()' under signal, and then 'MainWindow' under reciever, and 'close()' under slot. It's that easy :)

    *This example assumes a menu item named "Exit" created with that name initially, resulting in the auto name 'actionExit' and a window being used is a default main window with all the default settings. Adjust the parameters to suit your needs!!

    As of 3.4.1, there still is no direct 'wiring' of actions in the signals & slots editing interface (the red/blue lines on the widget itself). I think this a HUGE missing feature which for me makes that mode useless since more than half of my s&s editing involves actions. After all, what are most tasks initiated by users done from in a main UI window, that's right.. whether its a toolbar button or a menu item or statusbar or context menu, its AN ACTION. Why this wasn't included is... insert rant here. :) Hope that helps ease the pain and Im sorry it took 2 years to get a response from me (but hopefully new readers will be helped too). bye!

  • @dexli go to slot not showing triggered what to do

  • @Alok-Sinha

    lmao (don't get offended guys)
    After 8 years,
    Now he will use this answer

  • Lifetime Qt Champion


    Are you clicking it right ?
    alt text

Log in to reply