Qt5-控件-QMenu,QMenuBar-菜單欄詳解-菜單熱鍵-菜單校驗功能,qt5--qmenu
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QStatusBar>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QMenu* menu[10];
QAction* act[10];
QMenuBar* menuBar ;
QStatusBar* status ;
public slots:
void trigerMenu(QAction* act);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <Qt>
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->resize(400,300);
this->centralWidget();
status = new QStatusBar(this);
menu[0] = new QMenu("文件");
menu[0]->addAction("編輯");
menu[0]->addAction("查看");
menu[0]->addAction("工具");
act[0] = new QAction("新建",this);
act[0]->setShortcut(Qt::CTRL | Qt::Key_A );
act[0]->setStatusTip("這是一個新建菜單");
act[1] = new QAction("打開",this);
act[1]->setCheckable(true);
menu[1] = new QMenu("保存");
menu[1]->addAction(act[0]);
menu[1]->addAction(act[1]);
menu[2] = new QMenu("打印");
menu[2]->addAction("打印設置");
menu[2]->addMenu(menu[1]);
menuBar = new QMenuBar(this);
menuBar->addMenu(menu[0]);
menuBar->addMenu(menu[2]);
menuBar->setGeometry(0,0,this->width(),30);
connect(menuBar,SIGNAL(triggered(QAction*)),this,SLOT(trigerMenu(QAction*)));
}
MainWindow::~MainWindow()
{
}
void MainWindow::trigerMenu(QAction* act)
{
if(act->text() == "新建")
{
qDebug()<<"新建菜單被按下";
}
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}