From 9522ecd00763ba8a8a97908a919945dae7c64c65 Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 8 Aug 2023 17:43:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=B7=B1=E7=BB=83=E7=9A=84MenuBar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MenuBarTest/MenuBarTest.pro | 31 ++++++++ MenuBarTest/main.cpp | 11 +++ MenuBarTest/mainwindow.cpp | 137 ++++++++++++++++++++++++++++++++++++ MenuBarTest/mainwindow.h | 14 ++++ MenuBarTest/res.qrc | 3 + 5 files changed, 196 insertions(+) create mode 100644 MenuBarTest/MenuBarTest.pro create mode 100644 MenuBarTest/main.cpp create mode 100644 MenuBarTest/mainwindow.cpp create mode 100644 MenuBarTest/mainwindow.h create mode 100644 MenuBarTest/res.qrc diff --git a/MenuBarTest/MenuBarTest.pro b/MenuBarTest/MenuBarTest.pro new file mode 100644 index 0000000..4b66c5a --- /dev/null +++ b/MenuBarTest/MenuBarTest.pro @@ -0,0 +1,31 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + res.qrc diff --git a/MenuBarTest/main.cpp b/MenuBarTest/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/MenuBarTest/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/MenuBarTest/mainwindow.cpp b/MenuBarTest/mainwindow.cpp new file mode 100644 index 0000000..cbca132 --- /dev/null +++ b/MenuBarTest/mainwindow.cpp @@ -0,0 +1,137 @@ +#include "mainwindow.h" +#include +#include +/*-------------------------*/ +#include // 菜单栏 +#include // 菜单 +#include // 菜单项 +#include +/*-------------------------*/ +#include // 工具栏 +/*-------------------------*/ +#include // 状态栏 +/*-------------------------*/ +#include // 铆接部件 +/*-------------------------*/ +#include // 核心部件: 编辑器 +/*-------------------------*/ + + + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + // 第一部分: 5.1 菜单栏 + //**********窗口属性设置***************** + this->setWindowTitle("主窗口"); + this->setFixedSize(800, 600); + + //***********创建菜单栏****************** + //创建菜单栏 + //注意:如果只有菜单栏,则在窗口处没有现象 + QMenuBar *menu_bar = new QMenuBar(this); + this->setMenuBar(menu_bar); + + // 创建菜单 + QMenu *menu1 = new QMenu("文件",this); + menu_bar->addMenu(menu1); + + QMenu *menu2 = new QMenu("编辑",this); + menu_bar->addMenu(menu2); + + QMenu *menu3 = new QMenu("构建",this); + menu_bar->addMenu(menu3); + + // 创建菜单项 + QAction *act1 = new QAction("新建文件或项目",this); + menu1->addAction(act1); + + QAction *act2 = new QAction("打开文件或项目",this); + menu1->addAction(act2); + + // 添加分隔线 + menu1->addSeparator(); + + QAction *act3 = new QAction("保存", this); + menu1->addAction(act3); + + QAction *act4 = new QAction("另存为", this); + menu1->addAction(act4); + + QAction *act5 = new QAction("复制", this); + act5->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_C)); // 绑定快捷键 + menu2->addAction(act5); + + QAction *act6 = new QAction("粘贴", this); + act6->setShortcut(QKeySequence(tr("Ctrl+V"))); + menu2->addAction(act6); + + QAction *act7 = new QAction("剪切", this); + menu2->addAction(act7); + + // 当单机菜单项时,做出相应的处理 + connect(act5,&QAction::triggered,[=]()->void{ + qDebug()<<"复制正在执行"; + }); + + connect(act6,&QAction::triggered,[=]()->void{ + qDebug()<<"粘贴正在执行"; + }); + + + + // 第二部分: 5.2 工具栏 + //创建工具栏 + //注意:工具栏可以添加多个 + QToolBar *toolbar = new QToolBar(this); + //将工具栏添加到窗口 + // this->addToolBar(toolbar);//默认在最上面显示 + this->addToolBar(Qt::LeftToolBarArea, toolbar); //设置默认在左边显示 + + //工具栏添加菜单项和分割线 + QAction *act_tool1 = new QAction("欢迎", this); + QAction *act_tool2 = new QAction("编辑", this); + toolbar->addAction(act_tool1); + toolbar->addSeparator(); + toolbar->addAction(act_tool2); + + //设置工具栏的浮动状态,true:可以悬浮在窗口 false:不可以 + toolbar->setFloatable(true); + //设置运行工具栏的位置,设置为左边或者右边 + toolbar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea | Qt::TopToolBarArea | Qt::BottomToolBarArea); + + + // 第三部分: 5.3 状态栏 + //创建状态栏 + //状态栏只能有一个 + QStatusBar *statusbar = new QStatusBar(this); + //添加状态栏 + this->setStatusBar(statusbar); + + //给状态栏中添加文字信息或者按钮= + QLabel *label1 = new QLabel("左边信息", this); + statusbar->addWidget(label1); + + QLabel *label2 = new QLabel("右边信息", this); + statusbar->addPermanentWidget(label2); + + QPushButton *button = new QPushButton("设置", this); + statusbar->addWidget(button); + + + // 第四部分: 5.4 铆接部件 QDockWidget,也称浮动窗口,可以有多个。 + //创建铆接部件 + QDockWidget *dockwidget = new QDockWidget("这是一个铆接部件", this); + this->addDockWidget(Qt::TopDockWidgetArea, dockwidget); + + + // 第五部分: 5.5 核心部件(中心部件) + // 除了以上几个部件,中心显示的部件都可以作为核心部件,例如一个记事本文件,可以利用QTextEdit 做核心部件 + QTextEdit *edit = new QTextEdit("文本编辑器", this); + this->setCentralWidget(edit); +} + +MainWindow::~MainWindow() +{ +} + diff --git a/MenuBarTest/mainwindow.h b/MenuBarTest/mainwindow.h new file mode 100644 index 0000000..d147190 --- /dev/null +++ b/MenuBarTest/mainwindow.h @@ -0,0 +1,14 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); +}; +#endif // MAINWINDOW_H diff --git a/MenuBarTest/res.qrc b/MenuBarTest/res.qrc new file mode 100644 index 0000000..1358ce9 --- /dev/null +++ b/MenuBarTest/res.qrc @@ -0,0 +1,3 @@ + + +