自己练的MenuBar
This commit is contained in:
parent
1464145959
commit
9522ecd007
|
@ -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
|
|
@ -0,0 +1,11 @@
|
|||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
#include "mainwindow.h"
|
||||
#include <QPushButton>
|
||||
#include <QLabel>
|
||||
/*-------------------------*/
|
||||
#include <QMenuBar> // 菜单栏
|
||||
#include <QMenu> // 菜单
|
||||
#include <QAction> // 菜单项
|
||||
#include <QDebug>
|
||||
/*-------------------------*/
|
||||
#include <QToolBar> // 工具栏
|
||||
/*-------------------------*/
|
||||
#include <QStatusBar> // 状态栏
|
||||
/*-------------------------*/
|
||||
#include <QDockWidget> // 铆接部件
|
||||
/*-------------------------*/
|
||||
#include <QTextEdit> // 核心部件: 编辑器
|
||||
/*-------------------------*/
|
||||
|
||||
|
||||
|
||||
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()
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
};
|
||||
#endif // MAINWINDOW_H
|
|
@ -0,0 +1,3 @@
|
|||
<RCC>
|
||||
<qresource prefix="/images"/>
|
||||
</RCC>
|
Loading…
Reference in New Issue