Qt之QMenu使用

 

 

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 //
 6 #include <QMenu>
 7 #include <QMenuBar>
 8 #include <QToolBar>
 9 #include <QAction>
10 #include <QMessageBox>
11 #include <QDebug>
12 #include <QLabel>
13 
14 QT_BEGIN_NAMESPACE
15 namespace Ui { class MainWindow; }
16 QT_END_NAMESPACE
17 
18 class MainWindow : public QMainWindow
19 {
20     Q_OBJECT
21 
22 public:
23     MainWindow(QWidget *parent = nullptr);
24     ~MainWindow();
25 
26     void craeteMenuBar();
27     void craeteToolBar();
28     void craeteStatusBar();
29 public slots:
30     void clicknewMenuAction();
31     void clickopenMenuAction();
32 private:
33     QMenu*     m_menu;
34     QAction*   m_newMenu;
35     QAction*   m_openMenu;
36 
37     Ui::MainWindow *ui;
38 };
39 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9 
10     craeteMenuBar();
11 }
12 
13 MainWindow::~MainWindow()
14 {
15     delete ui;
16 }
17 
18 void MainWindow::craeteMenuBar()
19 {
20     m_menu = menuBar()->addMenu(tr("文件"));
21 
22     m_newMenu = new QAction(QIcon("://images/new.png"), "new", this);
23     m_openMenu = new QAction(QIcon("://images/open.png"), "open", this);
24 
25     m_newMenu->setShortcut(QKeySequence("Ctrl+N"));
26     m_openMenu->setShortcut(QKeySequence("Ctrl+O"));
27 
28     m_newMenu->setStatusTip(QStringLiteral("new"));
29     m_openMenu->setStatusTip(QStringLiteral("open"));
30 
31     m_menu->addAction(m_newMenu);
32     m_menu->addAction(m_openMenu);
33 
34     connect(m_newMenu,SIGNAL(triggered()),this,SLOT(clicknewMenuAction()));
35     connect(m_openMenu,SIGNAL(triggered()),this,SLOT(clickopenMenuAction()));
36 }
37 
38 void MainWindow::clicknewMenuAction()
39 {
40     QMessageBox MyBox(QMessageBox::Question,"Title","newMenu",QMessageBox::Yes | QMessageBox::No);
41     MyBox.exec();
42 }
43 
44 void MainWindow::clickopenMenuAction()
45 {
46     QMessageBox MyBox(QMessageBox::Question,"Title","openMenu",QMessageBox::Yes | QMessageBox::No);
47     MyBox.exec();
48 }
View Code

mainwindow.ui

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ui version="4.0">
 3  <class>MainWindow</class>
 4  <widget class="QMainWindow" name="MainWindow">
 5   <property name="geometry">
 6    <rect>
 7     <x>0</x>
 8     <y>0</y>
 9     <width>450</width>
10     <height>211</height>
11    </rect>
12   </property>
13   <property name="windowTitle">
14    <string>MainWindow</string>
15   </property>
16   <widget class="QWidget" name="centralwidget"/>
17   <widget class="QMenuBar" name="menubar">
18    <property name="geometry">
19     <rect>
20      <x>0</x>
21      <y>0</y>
22      <width>450</width>
23      <height>22</height>
24     </rect>
25    </property>
26   </widget>
27   <widget class="QStatusBar" name="statusbar"/>
28  </widget>
29  <resources/>
30  <connections/>
31 </ui>
View Code

 

posted on 2022-12-13 10:36  疯狂delphi  阅读(77)  评论(0编辑  收藏  举报

导航