×
Namespaces

Variants
Actions

Archived:Qt Dynamic Menus

From Nokia Developer 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
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
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);
iMenuBar->addAction(iAddAct);
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);
iFileMenu->addAction(iOpenAct);
connect(iOpenAct, SIGNAL(triggered()),this, SLOT(openFile()));
 
iSaveAct = new QAction(tr("&Save"), this);
iFileMenu->addAction(iSaveAct);
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:

iFileMenu->setEnabled(false);
iAddAct->setEnabled(false);

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

iFileMenu->setEnabled(true);
iAddAct->setEnabled(true);

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:

iFileMenu->setTitle(tr("&Tools"));

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

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

DynMenu.JPG


Example project

File:QTDynamicMenu.zip

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