QT basic menu bar

Time:2021-11-30

This chapter introduces the relevant operations of QT menu bar, which is divided into three parts:1. Introduction to relevant classes in the menu bar; 2. Generation and response of system menu; 3. Generation and response of pop-up menu; The menu bar is usually used only in programs based on qmainwindow. Programs based on QWidget usually have no menu bar.

1. Menu related

  QMenuBar –Menu bar class, that is, the red area mark in the figure below. The menu bar class provides a horizontal menu bar for the window. This menu bar occupies the area above the window, the vertical height remains unchanged, and the horizontal width is the window width, which can change with the size of the window. As shown in the following figure, the columns of “test”, “test1” and “test2” are qmenubar

  QMenu –Menu items, that is, the green area in the figure below. In the figure below, “test”, “test1” and “test2” are independent menus, including various submenus.Qmenu can also be used to create pop-up menus

  QAction –Submenu refers to the contents marked in the blue area in the figure below. One submenu corresponds to one operation.

2. Generation and response of system menu

2.1. Add system menu using code

Sample source code:

. h file

1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include 
 5 #include 
 6 
 7 QT_BEGIN_NAMESPACE
 8 namespace Ui { class MainWindow; }
 9 QT_END_NAMESPACE
10 
11 class MainWindow : public QMainWindow
12 {
13     Q_OBJECT
14 
15 public:
16     MainWindow(QWidget *parent = nullptr);
17     ~MainWindow();
18 
19 // menu bar
20     QMenuBar *pMenuBar;
21 // menu item
22     QMenu *pMenuTest;
23     QMenu *pMenuTest1;
24     QMenu *pMenuTest2;
25 // submenu
26     QAction *pActionTest;
27     QAction *pActionTest1;
28     QAction *pActionTest2;
29     QAction *pActionTest3;
30     QAction *pActionTest4;
31     QAction *pActionTest5;
32     QAction *pActionTest6;
33 
34 public slots:
35 // menu response function
36     void OnActionTest();
37     void OnActionTest1();
38 
39 private:
40     Ui::MainWindow *ui;
41 };
42 #endif // MAINWINDOW_H

. cpp file

 

1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include 
 4 
 5 // enable the menu bar of qmainwindow
 6 //#define USE_DEFAULT_MENU_BAR
 7 
 8 MainWindow::MainWindow(QWidget *parent)
 9     : QMainWindow(parent)
10     , ui(new Ui::MainWindow)
11 {
12     ui->setupUi(this);
13 
14 // specify the menu bar
15 #ifdef USE_DEFAULT_MENU_BAR
16 // add the menu bar (the menu provided by qmainwindow is added here)
17     pMenuBar = this->menuBar();
18 #else
19 // add custom menu
20     pMenuBar = new QMenuBar(this);
21 #endif
22 
23 // define menu items
24 // (& n) stands for shortcut. When the window gets focus, press Alt + n to open the "test" menu item
25 pmenutest = new qmenu ("test", this);
26     pMenuTest1 = new QMenu("test1",this);
27     pMenuTest2 = new QMenu("test2",this);
28 
29 // define submenu
30 // (& S) is the shortcut key of the submenu. When the menu item is opened, press the's' key to respond
31 pactiontest = new qaction ("test & S)", this);
32 // create a new menu item with an icon. The icon uses the resources in the resource file
33 pactiontest1 = new qaction (qicon (": / new / prefix1 / resource / soccer_ball. ICO"), "test 1", this);
34 pactiontest2 = new qaction ("test 2", this);
35 pactiontest3 = new qaction ("test 3 & Y)", this);
36 pactiontest4 = new qaction ("test 4", this);
37 pactiontest5 = new qaction ("test 5", this);
38 pactiontest6 = new qaction ("test 6", this);
39 
40 // add menu item to submenu
41     pMenuTest->addAction(pActionTest);
42     pMenuTest->addAction(pActionTest1);
43 // add a dividing line between menu items
44     pMenuTest->addSeparator();
45     pMenuTest->addAction(pActionTest2);
46 
47     pMenuTest1->addAction(pActionTest3);
48     pMenuTest1->addAction(pActionTest4);
49 
50     pMenuTest2->addAction(pActionTest5);
51     pMenuTest2->addAction(pActionTest6);
52 
53 // add submenu to menu bar
54     pMenuBar->addMenu(pMenuTest);
55     pMenuBar->addMenu(pMenuTest1);
56     pMenuBar->addMenu(pMenuTest2);
57 
58 #ifndef USE_DEFAULT_MENU_BAR
59 // this line must be added when qmainwindow's own menu bar is not used
60     setMenuBar(pMenuBar);
61 #endif
62 
63 // add menu response function
64     connect(pActionTest,&QAction::triggered,this,&MainWindow::OnActionTest);
65     connect(pActionTest1,&QAction::triggered,this,&MainWindow::OnActionTest);
66 }
67 
68 
69 // menu response function
70 void MainWindow::OnActionTest()
71 {
72     QDialog dlg;
73 dlg.setwindowtitle ("test menu response");
74     dlg.exec();
75 }
76 
77 void MainWindow::OnActionTest1()
78 {
79 
80     QDialog dlg;
81 dlg.setwindowtitle ("test menu response");
82     dlg.exec();
83 }
84 
85 MainWindow::~MainWindow()
86 {
87     delete ui;
88 }

 

The above code defines two ways to create system menus: call the menu bar provided by qmainwindow and create a new menu bar by yourself. It is controlled by the macro defined in line 6, and the display effect is the same.

In line 25, add shortcut keys to the “test” submenu. When the window gets focus, press Alt + n to expand the “test” submenu.

The 31st line adds shortcut keys to the “test” menu item of the “test” submenu. When the “test” submenu pops up, press the’s’ key, and the program can respond.

Line 33 adds an icon to the “test 2” menu item of the “test” submenu.

In line 44, a dividing line is added between “test 2” and “test 3” in the “test” submenu.

2.2. Use UI designer to design system menu

Enter the “design” page and enter the interface shown in the following figure. See the following two figures for specific operation methods,Note: after entering the menu name, you must press “enter” to take effect