55

I am completely new to Qt.

I started with a new Qt4 GUI Application.

Using the designer, I have created a menu like so:

File
 - Exit

How do I get an action associated with the menu item?

I found something called the 'Signals and slots editor' but have no idea how to use it.

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361

4 Answers4

54

Click on the green plus sign after you selected the signals Slots Editor. It will give you 4 fields to fill in. For sender you select what is creating the signal. For example ActionExit might be the name you created for the exit menu item. The signal is probably going to be clicked(). The receiver is usually the class that you created that has all of your methods. The slot is the method you created in that class that you want to execute. For example:

actionExit clicked() <nameOfClass> exitGame()

Hope this helps.

fbrereto
  • 35,429
  • 19
  • 126
  • 178
Isawpalmetto
  • 785
  • 3
  • 12
  • 18
  • Okay, great. One more quick question... what is the method signature for the function that gets called when the menu item is clicked? Is it like `void OnClick()`, or are there arguments to it? – Nathan Osman Apr 28 '10 at 02:18
  • 5
    Actually you would want to select triggered() because since it is an action. Now for the method that gets called, it does not need any arguments so you could call it void exitGame() if you want. – Isawpalmetto Apr 28 '10 at 02:23
  • 1
    By the way, I forgot to mention that triggered() is what should go in the signal field. Just wanted to be clear. – Isawpalmetto Apr 28 '10 at 02:26
  • 1
    Okay, I have files `mainwindow.cpp` and `mainwindow.h`. I added a `void` function but it won't show up in the slot field when I select `MainWindow` as the receiver... any ideas? – Nathan Osman Apr 28 '10 at 02:27
  • did you add it to the slots in the mainWindow.h file? – Isawpalmetto Apr 28 '10 at 02:29
  • @lsawpalmetto: Sorry, add it to the slots? How do I do that? – Nathan Osman Apr 28 '10 at 02:30
  • In the .h file you can add the data members and any slots. Check out some examples on the QT site, but it's basically like this: private slots: void exitGame(); Ant then in the mainWindow.cpp file you can write the method for exitGame() – Isawpalmetto Apr 28 '10 at 02:40
  • 1
    hello, is this(comment #7) how you managed to get the slot to show up? doesn't work for me. Also it would be appreciated if you could add the full details to the answer itself :) Thank you – Nisse Oct 10 '13 at 10:24
50

I managed to do this in a way that seems much easier. In the Qt Creator Action Editor window, i see an entry for the menu item I clicked. I rt-click that entry and select "Go to slot..." then i select triggered() from the popup and click OK. Qt Creator jumps me to the code it just added.... I put a qDebug statement in there and it works!

user347524
  • 501
  • 3
  • 2
  • 3
    The difference between this and the accepted answer is this answer creates an action specifically for the menu item and the accepted answer attaches to any pre specified slot, both aspects are useful. – radman Jun 10 '12 at 19:52
  • I misunderstood first, I mixed up what you said with the object browser, using the Action Editor is obviously the way to go! – Nisse Oct 11 '13 at 13:29
5

Go to the Slots Editor and then click on the Action Editor Tab on the bottem left side. There are all Menu Actions listed.

Right click -> go to Slot provides a slot function.

Darkproduct
  • 1,062
  • 13
  • 28
5

I have seen 2 may be 3 this kind of questions on this great forum but every one is very confusing,there is no need to go to signal/slot creator just got Qt Designer and follow the following steps

1.add Menu and action on menu bar and add any function in the slot of your mainwindow.h file as following private slots: void help();

2.Secondly add the following code in your mainwindow.cpp.

connect(ui->actionmyactions, SIGNAL(triggered()), this, SLOT(help()));

3.same can be done for menus as well using following code:

connect(ui->menuHelp, SIGNAL(aboutToShow()), this, SLOT(help()));

4.You can get the desired results without going to Qt Designer as following.

  1. declare your action in your mainwindow.h as following

    QAction *myaction;

  2. and add following code in your mainwindow.cpp

    myaction = ui->mainToolBar->addAction("help"); connect(myaction, SIGNAL(triggered()), this, SLOT(help()));

Shaikh Chili
  • 109
  • 1
  • 2
  • This helped me because I was trying to say ui->actionmyactions->triggered() instead of just triggered() when assigning the signal to slot. It seems like none of the Qt tutorials suggest to do it in code though, which is really annoying because I want to see what's actually happening -- they just want you to drag and drop and trust them like you're just a baby – Keegan Jay Nov 06 '22 at 20:21