Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect menu click with action in Qt Creator?

Tags:

action

qt

menu

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.

like image 537
Nathan Osman Avatar asked Apr 28 '10 02:04

Nathan Osman


People also ask

How do I use action editor in Qt Designer?

Enable the action editor by opening the Tools menu, and switching on the Action Editor option. The action editor allows you to create New actions and Delete actions. It also provides a search function, Filter, using the action's text. Qt Designer's action editor can be viewed in the classic Icon View and Detailed View.

How do I add menu bar to QT?

To access this QMenuBar and populate it with things of your choice, just call menuBar() from your QMainWindow instance. To add a submenu to a QMenuBar , use QMenuBar::addMenu .

What is an action in Qt?

Actions can be added to menus and toolbars, and will automatically keep them in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked. A QAction may contain an icon, menu text, a shortcut, status text, "What's This?" text, and a tooltip.


2 Answers

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.

like image 59
Isawpalmetto Avatar answered Sep 21 '22 06:09

Isawpalmetto


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!

like image 25
user347524 Avatar answered Sep 20 '22 06:09

user347524