1. 程式人生 > 其它 >Qt 選單欄QMenu、下拉選單QAction、工具欄QToolBar的使用

Qt 選單欄QMenu、下拉選單QAction、工具欄QToolBar的使用

如下內容是實現一個選單欄以及下拉選單的製作:
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 }