标签:
#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(); }
Qt5-控件-QMenu,QMenuBar-菜单栏详解-菜单热键-菜单校验功能
标签:
原文地址:http://www.cnblogs.com/shiyumiao/p/5208210.html