QT-菜单栏-状态栏-工具栏

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDockWidget>
#include <QLabel>
#include <QMenuBar>
#include <QPushButton>
#include <QStatusBar>
#include <QTextEdit>
#include <QToolBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 重置窗口大小
    this->setFixedSize(800, 600);

    // 创建菜单栏,源码已经挂在对象树,所以不需要制定对象树
    QMenuBar * bar = menuBar();
    // 将菜单栏放到窗口中
    setMenuBar(bar);
    // 创建菜单
    QMenu * fileMenu = bar->addMenu("文件");
    QMenu * editMenu = bar->addMenu("编辑");

    // 创建菜单项
    fileMenu->addAction("新建");
    // 添加分隔符
    fileMenu->addSeparator();
    fileMenu->addAction("打开");
    editMenu->addAction("***");

    // 创建工具栏,可以有多个,需要指定对象树
    QToolBar * toolBar = new QToolBar(this);
    addToolBar(Qt::LeftToolBarArea, toolBar);

    // 只允许左右停靠
    toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);
    toolBar->setFloatable(false);

    // 设置移动
    toolBar->setMovable(false);

    // 设置工具栏的内容
    QAction * newAction = fileMenu->addAction("新建");
    QAction * openAction = fileMenu->addAction("dakai");
    toolBar->addAction(newAction);
    toolBar->addSeparator();
    toolBar->addAction(openAction);

    // 工具栏添加空间
    QPushButton * btn = new QPushButton("aa", this);
    toolBar->addWidget(btn);

    // 状态栏
    QStatusBar * stBar = new QStatusBar(this);
    setStatusBar(stBar);
    QLabel * label = new QLabel("Message", this);
    stBar->addWidget(label);
    QLabel * label2 = new QLabel("Status", this);
    stBar->addPermanentWidget(label2);

    //  铆接部件(浮动窗口)
    QDockWidget * dock = new QDockWidget("浮动", this);
    addDockWidget(Qt::TopDockWidgetArea, dock);
    dock->setAllowedAreas(Qt::LeftDockWidgetArea);

    // 设置中心部件(核心窗口)
    QTextEdit * edit = new QTextEdit(this);
    setCentralWidget(edit);
}

MainWindow::~MainWindow()
{
    delete ui;
}


![](https://img2022.cnblogs.com/blog/2815986/202204/2815986-20220401221150166-78519496.png)








posted @ 2022-04-01 22:10  starc的miao  阅读(208)  评论(0)    收藏  举报