一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

如下内容是实现一个菜单栏以及下拉菜单的制作:
mainwindow.h中添加如下内容:

1 class QAction;
2 class QMenu;

pro中添加QT += widgetsQMenu类作为菜单栏,QAction类作为点击菜单栏的下拉菜单使用QMenu类对象的addaction(QAction类的对象)来添加:
main.c如下:

 1 #include "mainwindow.h"
 2 
 3 int main ( int argc, char *argv[] ) {
 4     Q_INIT_RESOURCE ( application ); /* 初始化qrc资源 */
 5     QApplication a ( argc, argv );
 6     a.setOrganizationName ( "Qtporject" );
 7     a.setApplicationName ( "Application  hello Example" ); /* 给窗口添加name */
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }

mainwindow.h如下:

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 class QPlainTextEdit;
 5 class QAction;
 6 class QMenu;
 7 
 8 class MainWindow : public QMainWindow {
 9     Q_OBJECT
10 public:
11     explicit MainWindow();
12     ~MainWindow();
13 private slots:
14     void test();
15 private:
16     void createActions();
17     void createMenus();
18     QPlainTextEdit *textEdit; /* 文本框 */
19     QMenu *fileMenu; /* file菜单 */
20     /* 定义了file的下拉菜单为new、open、save、saveAS、exit */
21     QAction *newAct;
22     QAction *open;
23     QAction *Save;
24     QAction *saveAs;
25     QAction *exit;
26 };
27 
28 #endif // MAINWINDOW_H

mainwindow.cpp如下:

 1 #include "mainwindow.h"
 2 
 3 MainWindow::MainWindow() {
 4     textEdit = new QPlainTextEdit; /* 创建一个文本编辑器 */
 5     setCentralWidget ( textEdit ); /* 设置文本编辑器为中心位置 */
 6     createActions(); /* 首先创建file下拉的菜单栏 */
 7     createMenus(); /* 再来创建菜单目录,把下拉菜单的内容addaction加入到menu中 */
 8 }
 9 
10 MainWindow::~MainWindow() {
11 }
12 
13 void MainWindow::test() {
14 }
15 
16 /* file下拉菜单,名字为new、open、Save、saveAs和exit,并且添加了快捷键和图标,以及相应的槽函数 */
17 void MainWindow::createActions() {
18     newAct = new QAction ( QIcon ( ":/image/new.png" ), tr ( "&New" ), this );
19     newAct->setShortcuts ( QKeySequence::New );
20     newAct->setStatusTip ( tr ( "Create a new file" ) );
21     connect ( newAct, SIGNAL ( triggered() ), this, SLOT ( test() ) ); /* New点击事件调用test */
22     open = new QAction ( QIcon ( ":/image/open.png" ), tr ( "&Open" ), this );
23     open->setShortcuts ( QKeySequence::Open );
24     open->setStatusTip ( tr ( "open a new file" ) );
25     connect ( open, SIGNAL ( triggered() ), this, SLOT ( test() ) ); /* 点击Open事件调用test */
26     Save = new QAction ( QIcon ( ":/image/save.png" ), tr ( "&Save" ), this );
27     Save->setShortcuts ( QKeySequence::Save );
28     Save->setStatusTip ( tr ( "Save a new file" ) );
29     connect ( Save, SIGNAL ( triggered() ), this, SLOT ( test() ) ); /* 点击Save事件调用test */
30     saveAs = new QAction ( tr ( "&SaveAs" ), this );
31     saveAs->setShortcuts ( QKeySequence::SaveAs );
32     newAct->setStatusTip ( tr ( "SaveAs a new file" ) );
33     connect ( saveAs, SIGNAL ( triggered() ), this, SLOT ( test() ) ); /* saveAS点击事件调用test */
34     exit = new QAction ( tr ( "&exit" ), this );
35     exit->setShortcuts ( QKeySequence::Close );
36     exit->setStatusTip ( tr ( "exit a new file" ) );
37     connect ( exit, SIGNAL ( triggered() ), this, SLOT ( close() ) ); /* 点击exit事件调用test */
38 }
39 
40 void MainWindow::createMenus() {
41     fileMenu = menuBar()->addMenu ( tr ( "&File" ) ); /* 创建一个name为file的菜单栏 */
42     /* 在这个菜单栏添加action即下拉菜单 */
43     fileMenu->addAction ( newAct );
44     fileMenu->addAction ( open );
45     fileMenu->addAction ( Save );
46     fileMenu->addAction ( saveAs );
47     fileMenu->addSeparator(); /* 添加一个分割器 */
48     fileMenu->addAction ( exit ); /* 推出下拉菜单 */
49 }

 下文在原来的基础上添加了菜单栏edithelp,以及edithelp的下拉菜单,还添加了file工具栏和edit工具栏。菜单栏及下拉菜单与上面使用的一样,创建菜单栏类如下:

1 QMenu *fileMenu;
2 QMenu *editMenu;
3 QMenu *helpMenu;

下拉菜单的类如下:

 1 /* 定义edit的下拉菜单cut、copy、past */
 2 QAction *cut;
 3 QAction *copy;
 4 QAction *past;
 5 /* 定义help的下拉菜单about、aboutQt */
 6 QAction *about;
 7 QAction *aboutQt;
 8 edit与help的使用方法为:
 9 /* 创建一个name为edit的菜单栏 */
10 editMenu = menuBar()->addMenu ( tr ( "&Edit" ) );
11 /* 在这个菜单栏添加action,即下拉菜单 */
12 editMenu->addAction ( cut );
13 editMenu->addAction ( copy );
14 editMenu->addAction ( past );
15 
16 /* 创建一个name为help的菜单栏 */
17 helpMenu = menuBar()->addMenu ( tr ( "&Help" ) );
18 /* 在这个菜单栏添加action,即下拉菜单 */
19 helpMenu->addAction ( about );
20 helpMenu->addAction ( aboutQt );

工具栏使用的使用如下:

1 QToolBar *fileToolBar;
2 QToolBar *editToolBar;

使用方法为:

1 fileToolBar = addToolBar ( tr ( "File" ) );
2 fileToolBar->addAction ( newAct );
3 fileToolBar->addAction ( open ) ;
4 fileToolBar->addAction ( Save );
5 editToolBar = addToolBar ( tr ( "Edit" ) );
6 editToolBar->addAction ( cut );
7 editToolBar->addAction ( copy );
8 editToolBar->addAction ( past );

mainwindow.h如下:

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 class QPlainTextEdit;
 5 class QAction;
 6 class QMenu;
 7 
 8 class MainWindow : public QMainWindow{
 9     Q_OBJECT
10 public:
11     explicit MainWindow();
12     ~MainWindow();
13 private slots:
14     void test();
15 private:
16     void createActions();
17     void createMenus();
18     void createToolBars();
19 
20     QPlainTextEdit *textEdit; /* 文本框 */
21     /* file菜单 */
22     QMenu *fileMenu;
23     QMenu *editMenu;
24     QMenu *helpMenu;
25     QToolBar *fileToolBar;
26     QToolBar *editToolBar;
27     /* 定义了file的下拉菜单为new、open、save、saveAS和exit */
28     QAction *newAct;
29     QAction *open;
30     QAction *Save;
31     QAction *saveAs;
32     QAction *exit;
33     /* 定义edit的下拉菜单cut、copy、past */
34     QAction *cut;
35     QAction *copy;
36     QAction *past;
37     /* 定义help的下拉菜单about aboutQt */
38     QAction *about;
39     QAction *aboutQt;
40 };
41 
42 #endif // MAINWINDOW_H

mainwindow.cpp代码:

 1 #include "mainwindow.h"
 2 
 3 MainWindow::MainWindow() {
 4     textEdit = new QPlainTextEdit;
 5     setCentralWidget ( textEdit );
 6     createActions(); /* 首先创建file下拉的菜单栏 */
 7     createMenus(); /* 再来创建菜单目录,把下拉菜单的内容addaction加入到menu中 */
 8     createToolBars();
 9 }
10 
11 MainWindow::~MainWindow() {
12 }
13 
14 void MainWindow::test() {
15 }
16 
17 void MainWindow::createActions() {
18     newAct = new QAction ( QIcon ( ":/image/new.png" ), tr ( "&New" ), this );
19     newAct->setShortcuts ( QKeySequence::New );
20     newAct->setStatusTip ( tr ( "Create a new file" ) );
21     connect ( newAct, SIGNAL ( triggered() ), this, SLOT ( test() ) );
22     open = new QAction ( QIcon ( ":/image/open.png" ), tr ( "&Open" ), this );
23     open->setShortcuts ( QKeySequence::Open );
24     open->setStatusTip ( tr ( "open a new file" ) );
25     connect ( open, SIGNAL ( triggered() ), this, SLOT ( test() ) );
26     Save = new QAction ( QIcon ( ":/image/save.png" ), tr ( "&Save" ), this );
27     Save->setShortcuts ( QKeySequence::Save );
28     Save->setStatusTip ( tr ( "Save a new file" ) );
29     connect ( Save, SIGNAL ( triggered() ), this, SLOT ( test() ) );
30     saveAs = new QAction ( tr ( "&SaveAs" ), this );
31     saveAs->setShortcuts ( QKeySequence::SaveAs );
32     newAct->setStatusTip ( tr ( "SaveAs a new file" ) );
33     connect ( saveAs, SIGNAL ( triggered() ), this, SLOT ( test() ) );
34     exit = new QAction ( tr ( "&exit" ), this );
35     exit->setShortcuts ( QKeySequence::Close );
36     exit->setStatusTip ( tr ( "exit a new file" ) );
37     connect ( exit, SIGNAL ( triggered() ), this, SLOT ( close() ) );
38     cut = new QAction ( QIcon ( ":/image/cut.png" ), tr ( "&Cut" ), this );
39     cut->setShortcuts ( QKeySequence::Cut );
40     cut->setStatusTip ( tr ( "Save a new file" ) );
41     connect ( cut, SIGNAL ( triggered() ), this, SLOT ( test() ) );
42     copy = new QAction ( QIcon ( ":/image/copy.png" ), tr ( "&Copy" ), this );
43     saveAs->setShortcuts ( QKeySequence::Copy );
44     copy->setStatusTip ( tr ( "SaveAs a new file" ) );
45     connect ( copy, SIGNAL ( triggered() ), this, SLOT ( test() ) );
46     past = new QAction ( QIcon ( ":/image/paste.png" ), tr ( "&Paste" ), this );
47     past->setShortcuts ( QKeySequence::Paste );
48     past->setStatusTip ( tr ( "exit a new file" ) );
49     connect ( past, SIGNAL ( triggered() ), this, SLOT ( close() ) );
50     about = new QAction ( tr ( "&about" ), this );
51     about->setStatusTip ( tr ( "SaveAs a new file" ) );
52     connect ( about, SIGNAL ( triggered() ), this, SLOT ( test() ) );
53     aboutQt = new QAction ( tr ( "&aboutQt" ), this );
54     aboutQt->setStatusTip ( tr ( "exit a new file" ) );
55     connect ( aboutQt, SIGNAL ( triggered() ), this, SLOT ( close() ) );
56 }
57 
58 void MainWindow::createToolBars() {
59     fileToolBar = addToolBar ( tr ( "File" ) );
60     fileToolBar->addAction ( newAct );
61     fileToolBar->addAction ( open );
62     fileToolBar->addAction ( Save );
63     editToolBar = addToolBar ( tr ( "Edit" ) );
64     editToolBar->addAction ( cut );
65     editToolBar->addAction ( copy );
66     editToolBar->addAction ( past );
67 }
68 
69 void MainWindow::createMenus() {
70     fileMenu = menuBar()->addMenu ( tr ( "&File" ) ); /* 创建一个name为file的菜单栏 */
71     /* 在这个菜单栏添加action即下拉菜单 */
72     fileMenu->addAction ( newAct );
73     fileMenu->addAction ( open );
74     fileMenu->addAction ( Save );
75     fileMenu->addAction ( saveAs );
76     fileMenu->addSeparator(); /* 添加一个分割器 */
77     fileMenu->addAction ( exit ); /* 推出下拉菜单 */
78     editMenu = menuBar()->addMenu ( tr ( "&Edit" ) ); /* 创建一个name为edit的菜单栏 */
79     /* 在这个菜单栏添加action即下拉菜单 */
80     editMenu->addAction ( cut );
81     editMenu->addAction ( copy );
82     editMenu->addAction ( past );
83     helpMenu = menuBar()->addMenu ( tr ( "&Help" ) ); /* 创建一个name为help的菜单栏 */
84     /* 在这个菜单栏添加action即下拉菜单 */
85     helpMenu->addAction ( about );
86     helpMenu->addAction ( aboutQt );
87 }

 

posted on 2022-05-18 16:26  一杯清酒邀明月  阅读(3763)  评论(1编辑  收藏  举报