Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:Qt Dynamic Menus

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

This article shows you how to construct a menu for your application dynamically, instead of using the QMainWindow approach outlined in Qt Menus and Archived:How to create a menu in Qt.

Article Metadata
Code ExampleTested with
Devices(s): S60 Emulator
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Platform Security
Signing Required: Self-Signed
Capabilities: None
Keywords: QMenu,QAction,
Created: symbianyucca (10 Apr 2009)
Last edited: hamishwillee (11 Oct 2012)

Dynamic Menu with Qt

You can create your own menu by constructing QMenuBar as follows:

iMenuBar = new QMenuBar(this);

Menu items are added as QAction, so to make a new menu item you construct a new QAction, add it to the menu-bar with addAction function, and then connect the triggered signal from the QAction menuitem to the handler slot function:

iAddAct = new QAction(tr("&Add"), this);
connect(iAddAct, SIGNAL(triggered()),this, SLOT(addEntry()));

To make a sub menu, you need to instead construct a QMenu item and add it to the manubar, and then add the QAction menu items into the QMenu item. For example to make a file sub-menu that has two items you could add following code into your project:

iFileMenu = iMenuBar->addMenu(tr("&File"));
iOpenAct = new QAction(tr("&Open"), this);
connect(iOpenAct, SIGNAL(triggered()),this, SLOT(openFile()));
iSaveAct = new QAction(tr("&Save"), this);
connect(iSaveAct, SIGNAL(triggered()),this, SLOT(saveFile()));

Removing the menu items are handled with setEnabled() function for both menu items and submenus. For example if you want to remove the iFileMenu & iAddAct from the menu, you could call following two lines of code:


And to re-enable them and get them showing again, you could do as follows:


Changing the menu items text, is differently with submenus and menu items, QMenu sub menu items text can be changed with setTitle() function, for example if you want to change the “File” to be “Tools”, you could do it like this:


And for the menu items changing “Add” to “Something else”, can be handled with setText function:

iAddAct ->setText(tr("&Something else"));


Example project

This page was last modified on 11 October 2012, at 01:14.
86 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.