Archived:How to create a menu in Qt
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
This code snippet demonstrates how to create a basic menu in Qt - for more detailed information see Qt Menus. If you need to handle menu dynamically, then check out Archived:Qt Dynamic Menus
Article Metadata
Code Example
Tested with
Compatibility
Platform Security
Article
Contents |
Preconditions
- Download and install the Qt SDK
Procedure
- Create a New Qt project with GUI main window.
- Copy the code given below in the corresponding source and header file.
Various Function
- This property holds whether the menu supports being torn off.When true, the menu contains a special tear-off item (often shown as a dashed line at the top of the menu) that creates a copy of the menu when it is triggered.
bar->setTearOffEnabled(1);
- This property specifies whether consecutive separators in the menu should be visually collapsed to a single one. Separators at the beginning or the end of the menu are also hidden.
bar->setSeparatorsCollapsible(true);
Source File
#include "menumainwindow.h"
menumainwindow::menumainwindow(QWidget *parent)
: QMainWindow(parent)
{
label1= new QLabel("Hello");
setCentralWidget(label1);
showAct = new QAction(("&Show"), this);
connect(showAct, SIGNAL(triggered()),label1, SLOT(show()));
hideAct = new QAction(("&Hide"), this);
connect(hideAct, SIGNAL(triggered()),label1, SLOT(hide()));
exitAct = new QAction(("&Exit"), this);
connect(exitAct, SIGNAL(triggered()),qApp, SLOT(quit()));
bar = menuBar()->addMenu("&File");
bar->addAction(showAct);
bar->addAction(hideAct);
bar->addSeparator();
bar->addAction(exitAct);
}
menumainwindow::~menumainwindow()
{
}
Screenshot
Modified code snippet
Header File
#ifndef MENUMAINWINDOW_H
#define MENUMAINWINDOW_H
#include <QtGui/QMainWindow>
#include "ui_menumainwindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QLabel>
#include <QAction>
class menumainwindow : public QMainWindow
{
Q_OBJECT
public:
menumainwindow(QWidget *parent = 0);
~menumainwindow();
private:
QLabel* label1;
QMenu *bar;
QAction* showAct;
QAction* hideAct;
QAction* exitAct;
};
#endif // MENUMAINWINDOW_H
Souece Code
Mainmenu::Mainmenu(QWidget *parent)
: QMainWindow(parent)
{
//label1= new QLabel();
line=new QLineEdit("Hello");
setCentralWidget(line);
//setCentralWidget(label1);
showAct = new QAction(("&Show"), this);
connect(showAct, SIGNAL(triggered()),line, SLOT(show()));
hideAct = new QAction(("&Hide"), this);
connect(hideAct, SIGNAL(triggered()),line, SLOT(hide()));
exitAct = new QAction(("&Exit"), this);
connect(exitAct, SIGNAL(triggered()),qApp, SLOT(quit()));
cut=new QAction(("&Cut"),this);
connect(cut,SIGNAL(triggered()),line,SLOT(cut()));
copy=new QAction(("C&opy"),this);
connect(copy,SIGNAL(triggered()),line,SLOT(copy()));
paste=new QAction(("&Paste"),this);
connect(paste,SIGNAL(triggered()),line,SLOT(paste()));
bar = menuBar()->addMenu("&File");
bar1=menuBar()->addMenu("&Edit");
bar->setTearOffEnabled(1);
bar->addAction(showAct);
bar->addSeparator();
bar->setSeparatorsCollapsible(true);
bar->addAction(hideAct);
bar->addSeparator();
bar->addAction(exitAct);
bar1->addAction(cut);
bar1->addAction(copy);
bar1->addSeparator();
bar1->addAction(paste);
}
Mainmenu::~Mainmenu()
{
delete label1;
delete bar;
delete showAct;
delete hideAct;
delete exitAct;
}
Header File
#ifndef MAINMENU_H
#define MAINMENU_H
#include <QtGui/QMainWindow>
#include <QMenu>
#include <QLabel>
#include <QAction>
#include<QLineEdit>
namespace Ui
{
class MainmenuClass;
}
class Mainmenu : public QMainWindow
{
Q_OBJECT
public:
Mainmenu(QWidget *parent = 0);
~Mainmenu();
private:
QLabel* label1;
QMenu *bar;
QMenu *bar1;
QLineEdit *line;
QAction* showAct;
QAction* hideAct;
QAction* exitAct;
QAction* copy;
QAction* cut;
QAction* paste;
};
Note:This program is executed in Qt creator IDE V4.5
Handling Menu Actions in Qt
The class QMenuBar is used to create menu in Qt. But if you create a basic Qt GUI application with Main Window, then QMainWindow will provides a member function called menuBar() which creates and returns an empty menu bar if the menu bar does not exist. if application does not use QMainWindow, then you need to construct menu using QMenuBar, for this case check Archived:Qt Dynamic Menus.
The function QMenuBar::addAction() creates a new action (QAction) with text. The function adds the newly created action to the menu's list of actions, and returns it. QAction has a signal triggered() which is generated when user click on menu item, so basically we need to connect that triggered() signal to our slots to handle menu actions.
The code example shows how to handle menu actions.
Header file
#ifndef OPTIONSMENU_H
#define OPTIONSMENU_H
#include <QtGui/QMainWindow>
#include "ui_OptionsMenu.h"
class OptionsMenu : public QMainWindow
{
Q_OBJECT
public:
OptionsMenu(QWidget *parent = 0);
~OptionsMenu();
//function to create menu.
void createMyMenu();
public Q_SLOTS: //slots to receive action of menu trigger.
void helpAction();
void aboutAction();
void versionAction();
private:
Ui::OptionsMenuClass ui;
//Actions foe menu item
QAction* menu_helpAction;
QAction* menu_aboutAction;
QAction* menu_versionAction;
QAction* menu_exitAction;
};
#endif // OPTIONSMENU_H
Source file
#include "OptionsMenu.h"
#include <QMessageBox>
OptionsMenu::OptionsMenu(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//create menu
createMyMenu();
}
void OptionsMenu::createMyMenu()
{
//add menu item help to menu
menu_helpAction = new QAction(tr("Help"), this);
menuBar()->addAction(menu_helpAction);
connect(menu_helpAction, SIGNAL(triggered()), this, SLOT(helpAction()));
//add menu item help to About
menu_aboutAction = new QAction(tr("About"), this);
menuBar()->addAction(menu_aboutAction);
connect(menu_aboutAction, SIGNAL(triggered()), this, SLOT(aboutAction()));
//add menu item help to Version
menu_versionAction = new QAction(tr("Version"), this);
menuBar()->addAction(menu_versionAction);
connect(menu_versionAction, SIGNAL(triggered()), this, SLOT(versionAction()));
//add menu item help to Exit
menu_exitAction = new QAction(tr("Exit"), this);
menuBar()->addAction(menu_exitAction);
connect(menu_exitAction, SIGNAL(triggered()), this, SLOT(close()));
}
//Handle action for help menu.
void OptionsMenu::helpAction()
{
//do your task for help menu item.
QMessageBox msgBox;
msgBox.setInformativeText("What kind of help do you want?");
msgBox.setStandardButtons(QMessageBox::Ok);
int ret = msgBox.exec();
}
//Handle action for help About.
void OptionsMenu::aboutAction()
{
//do your task for help about item.
QMessageBox msgBox;
msgBox.setInformativeText("Testing menu in Qt.");
msgBox.setStandardButtons(QMessageBox::Ok);
int ret = msgBox.exec();
}
//Handle action for help Version.
void OptionsMenu::versionAction()
{
//do your task for version menu item.
QMessageBox msgBox;
msgBox.setInformativeText("Version -1.");
msgBox.setStandardButtons(QMessageBox::Ok);
int ret = msgBox.exec();
}
OptionsMenu::~OptionsMenu()
{
delete menu_helpAction;
delete menu_aboutAction;
delete menu_versionAction;
delete menu_exitAction;
}
Code Example
This Code Example is tested on Nokia 5800.




(no comments yet)