×
Namespaces

Variants
Actions

Archived:How to add submenu in Qt

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.

Article Metadata
Code Example
Source file: Media:QtSubMenu.zip
Tested with
Devices(s): Nokia 5800 XpressMusic
CompatibilityPlatform Security
Signing Required: Self-Signed
Article
Keywords: QMenu
Created: savaj (15 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This code snippet shows how to create sub-menu in Qt. For more information see Qt Menus

We need to call QMenu::addMenu() method to add submenu in existing menu item.

Headers files

#ifndef SUBMENU_H
#define SUBMENU_H
 
#include <QtGui/QMainWindow>
#include "ui_SubMenu.h"
 
class SubMenu : public QMainWindow
{
Q_OBJECT
 
public:
SubMenu(QWidget *parent = 0);
~SubMenu();
 
private slots:
void wikiForumNokia();
void DiBoForumNokia();
void nokiaIndia();
void nokiaEurope();
void help();
void about();
 
 
private:
void createMyMenus();
 
private:
Ui::SubMenuClass ui;
 
QMenu *nokiaMenu;
QMenu *forumNokiaMenu;
 
QAction *menu_nokiaIndiaAction;
QAction *menu_nokiaEuropeAction;
QAction *menu_wikiAction;
QAction *menu_DiBoAction;
QAction *menu_aboutAction;
QAction *menu_helpAction;
QAction *menu_exitAction;
};
 
#endif // SUBMENU_H

Source

#include "SubMenu.h"
#include <QMenu>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
 
SubMenu::SubMenu(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowTitle(tr("Sub Menus"));
//Create memu and sub menu.
createMyMenus();
}
 
void SubMenu::createMyMenus()
{
 
//add action for help menu.
menu_helpAction = new QAction(tr("Help"), this);
menuBar()->addAction(menu_helpAction);
connect(menu_helpAction, SIGNAL(triggered()), this, SLOT(help()));
 
//add action for about menu.
menu_aboutAction = new QAction(tr("About"), this);
menuBar()->addAction(menu_aboutAction);
connect(menu_aboutAction, SIGNAL(triggered()), this, SLOT(about()));
 
/* for nokia menu and its sub menus. */
//create action Nokia India sub menu item.
menu_nokiaIndiaAction = new QAction(tr("Nokia India"), this);
connect(menu_nokiaIndiaAction, SIGNAL(triggered()), this, SLOT(nokiaIndia()));
 
//create action Nokia Europe sum menu item
menu_nokiaEuropeAction = new QAction(tr("Nokia Europe"), this);
connect(menu_nokiaEuropeAction, SIGNAL(triggered()), this, SLOT(nokiaIndia()));
 
//add menu item nokia to main menu
nokiaMenu = menuBar()->addMenu(tr("Nokia"));
//add action for nokia india sub-menu
nokiaMenu->addAction(menu_nokiaIndiaAction);
//add action for nokia india sub-menu
nokiaMenu->addAction(menu_nokiaEuropeAction);
 
//create action for Discussion Boards sub menu item
menu_DiBoAction = new QAction(tr("Discussion Boards"), this);
connect(menu_DiBoAction, SIGNAL(triggered()), this, SLOT(DiBoForumNokia()));
 
//create action for Wiki sub menu item
menu_wikiAction = new QAction(tr("Wiki"), this);
connect(menu_wikiAction, SIGNAL(triggered()), this, SLOT(wikiForumNokia()));
 
//add menu item Nokia Developer to menu item nokia
forumNokiaMenu = nokiaMenu->addMenu(tr("Nokia Developer"));
//add action for Discussion Boards sub-menu
forumNokiaMenu->addAction(menu_DiBoAction);
//add action for Wiki sub-menu
forumNokiaMenu->addAction(menu_wikiAction);
 
//Add action for exit menu.
menu_exitAction = new QAction(tr("Exit"), this);
menuBar()->addAction(menu_exitAction);
connect(menu_exitAction, SIGNAL(triggered()), this, SLOT(close()));
 
 
}
 
/* handle action for wiki sub-menu item. */
void SubMenu::wikiForumNokia()
{
QDesktopServices::openUrl(QUrl(tr("http://www.developer.nokia.com/Community/Wiki/Wiki_Home")));
}
 
/* handle action for Discussion Boards sub-menu item. */
void SubMenu::DiBoForumNokia()
{
QDesktopServices::openUrl(QUrl(tr("http://www.developer.nokia.com/Community/Discussion/")));
}
 
/* handle action for nokia india sub-menu item. */
void SubMenu::nokiaIndia()
{
QDesktopServices::openUrl(QUrl(tr("http://www.nokia.co.in/")));
}
 
/* handle action for nokia europe sub-menu item. */
void SubMenu::nokiaEurope()
{
QDesktopServices::openUrl(QUrl(tr("http://europe.nokia.com/home")));
}
 
/* handle action for help menu item. */
void SubMenu::help()
{
QMessageBox::information(this, tr("Help Menu"),tr("This example shows how to create submenu."));
}
 
/* handle action for about menu item. */
void SubMenu::about()
{
QMessageBox::about(this, tr("About Menu"),tr("Testing Sub-Menus."));
}
 
 
SubMenu::~SubMenu()
{
 
}

Postconditions

The code snippet is expected to show sum-menus in application.
Submenu1.JPG

Submenu2.JPG


See also

Code Example

  • File:QtSubMenu.zip will show sub-menu in application and is tested on Nokia 5800 XpressMusic.
This page was last modified on 11 October 2012, at 01:14.
146 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.

×