#include <QMenu>
#include <QAction>
#include <QMenuBar>
#include <QLabel>
#include <QWidget>
#include <QVBoxLayout>
#include <QStatusBar>
#include <QKeySequence>
#include <QMessageBox>
#include "mainwindow.h"
MainWindow::MainWindow()
{
QWidget *widget= new QWidget;
setCentralWidget(widget); //设置中央Widget
infoLabel= new QLabel( "Choose a menu option,or right-click to invoke action");
infoLabel->setAlignment(Qt::AlignCenter); //居中对齐
QVBoxLayout *layout= new QVBoxLayout;
layout->addWidget(infoLabel);
widget->setLayout(layout);
createActions();
createMenus();
QString msg= "popup a menu by right-clicking.";
statusBar()->showMessage(msg);
setWindowTitle(tr( "Menus"));
setMinimumSize(160,160); //设置最小尺寸
resize(480,320); //设置尺寸
}
//创建Actions
void MainWindow::createActions()
{
newAct= new QAction( "&New", this);
newAct->setShortcut(QKeySequence( "Ctrl+N")); //设置快捷键
newAct->setStatusTip( "New a file"); //设置状态栏提示
connect(newAct,SIGNAL(triggered()), this,SLOT(newFile()));
openAct = new QAction( "&Open...", this);
openAct->setShortcut(tr( "Ctrl+O"));
openAct->setStatusTip( "Open an existing file");
connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));
saveAct = new QAction( "&Save", this);
saveAct->setShortcut(tr( "Ctrl+S"));
saveAct->setStatusTip( "save the file");
connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile()));
printAct = new QAction( "&Print...", this);
printAct->setShortcut(tr( "Ctrl+P"));
printAct->setStatusTip( "Print the file");
connect(printAct, SIGNAL(triggered()), this, SLOT(printFile()));
exitAct = new QAction( "E&xit", this);
exitAct->setShortcut(tr( "Ctrl+Q"));
exitAct->setStatusTip( "Exit the Application.");
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
boldAct = new QAction(tr( "&Bold"), this);
boldAct->setCheckable( true); //可选择
boldAct->setShortcut(tr( "Ctrl+B"));
boldAct->setStatusTip(tr( "Make the text bold"));
connect(boldAct, SIGNAL(triggered()), this, SLOT(bold()));
QFont boldFont = boldAct->font();
boldFont.setBold( true); //字体加粗
boldAct->setFont(boldFont); //设置字体
italicAct = new QAction(tr( "&Italic"), this);
italicAct->setCheckable( true);
italicAct->setShortcut(tr( "Ctrl+I"));
italicAct->setStatusTip(tr( "Make the text italic"));
connect(italicAct, SIGNAL(triggered()), this, SLOT(italic()));
QFont italicFont = italicAct->font();
italicFont.setItalic( true); //设置倾斜
italicAct->setFont(italicFont);
aboutAct= new QAction( "&About", this);
aboutAct->setStatusTip( "About the App");
connect(aboutAct,SIGNAL(triggered()), this,SLOT(about()));
}
//创建菜单
void MainWindow::createMenus()
{
//文件菜单
fileMenu=menuBar()->addMenu( "&File");
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(printAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
//编辑菜单
editMenu=menuBar()->addMenu( "&Edit");
formatMenu=editMenu->addMenu( "&Format");
formatMenu->addAction(boldAct);
formatMenu->addAction(italicAct);
//帮助菜单
helpMenu=menuBar()->addMenu( "&Help");
helpMenu->addAction(aboutAct);
}
void MainWindow::newFile()
{
infoLabel->setText( "Invoke<b>File|New</b>");
}
void MainWindow::openFile()
{
infoLabel->setText( "Invoke<b>File|Open</b>");
}
void MainWindow::saveFile()
{
infoLabel->setText( "Invoke<b>File|Save</b>");
}
void MainWindow::printFile()
{
infoLabel->setText( "Invoke<b>File|Print</b>");
}
//关于对话框
void MainWindow::about()
{
infoLabel->setText( "Invoke<b>Help|About</b>");
QMessageBox::about( this, "About", "How to createMenus");
}
void MainWindow::bold()
{
infoLabel->setText( "Invoke<b>Edit|Format|bold</b>");
}
void MainWindow::italic()
{
infoLabel->setText( "Invoke<b>Edit|Format|italic</b>");
}