Qt菜单栏、工具栏、状态栏(右键)

1.菜单栏(可以有多个)

cpp 复制代码
void MenuAndToolsAndStatus::Init_Menu()
{
    QMenuBar* bar = menuBar();
    this->setMenuBar(bar); // 将菜单栏放入主窗口
    QMenu* fileMenu = bar->addMenu("文件"); // 创建父节点
    // 添加子菜单
    QAction* newAction = fileMenu->addAction("新建文件"); // 设置名字
    newAction->setShortcut(Qt::CTRL | Qt::Key_A); // 设置快捷键ctrl+a
    fileMenu->addSeparator(); // 添加分割线

    QAction* openAction = fileMenu->addAction("打开文件"); // 设置名字
    openAction->setShortcut(Qt::CTRL | Qt::Key_C); // 设置快捷键ctrl+c // 创建工具栏 (可屏蔽掉 屏蔽掉后底部将失去控件栏位)
    //右击弹出菜单
    fileMenu->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(fileMenu, &QMenu::customContextMenuRequested, this, [=](const QPoint pos) {
        QMenu contextMenu;
        QAction* action = new QAction(tr("选项1"), this);
        QAction* action2 = new QAction(tr("选项2"), this);
        contextMenu.addAction(action);
        contextMenu.addAction(action2);
        //点击
        connect(action, &QAction::triggered, this, [=]() {
            QMessageBox::information(nullptr, "提示", "选项1", QMessageBox::Ok);
        });
        contextMenu.exec(mapToGlobal(pos));
    });

    // 绑定槽函数
    connect(newAction, &QAction::triggered, this, [=]() {
        QMessageBox::information(nullptr, "提示", "触发新建文件", QMessageBox::Ok);
    });
    connect(openAction, &QAction::triggered, this, [=]() {
        QMessageBox::information(nullptr, "提示", "触发打开文件", QMessageBox::Ok);
    });
}

2.工具栏(最多有一个)

cpp 复制代码
void MenuAndToolsAndStatus::Init_ToolBar()
{
    // ----------------------------------------------------------
    QToolBar* toolBar = new QToolBar(this); // 创建工具栏
    addToolBar(Qt::LeftToolBarArea, toolBar); // 设置默认停靠范围
    toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); // 允许上下拖动
    toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea); // 允许左右拖动

    toolBar->setFloatable(false); // 设置是否浮动
    toolBar->setMovable(false); // 设置工具栏不允许移动

    // 创建快捷项等同于菜单项
    QAction* newAction = new QAction("新建");
    QAction* openAction = new QAction("保存");

    // 工具栏添加菜单项
    toolBar->addAction(newAction);
    toolBar->addSeparator();
    toolBar->addAction(openAction);
    openAction->setToolTip("这是保存按钮");

    // ----------------------------------------------------------
    // 绑定槽函数
    // ----------------------------------------------------------
    connect(newAction, &QAction::triggered, this, [=]() {
        QMessageBox::information(nullptr, "提示", "触发新建文件", QMessageBox::Ok);
    });

    connect(openAction, &QAction::triggered, this, [=]() {
        QMessageBox::information(nullptr, "提示", "触发打开文件", QMessageBox::Ok);
    });
    // 设置上下文菜单策略
    toolBar->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(toolBar, &QToolBar::customContextMenuRequested, this, [&](const QPoint pos) {
        QMenu contextMenu(tr("上下文菜单"), this);
        QAction* action1 = new QAction(tr("选项1"), this);
        QAction* action2 = new QAction(tr("选项2"), this);

        contextMenu.addAction(action1);
        contextMenu.addAction(action2);

        connect(action1, &QAction::triggered, this, [=]() {
            QMessageBox::information(nullptr, "提示", "选项1", QMessageBox::Ok);
        });
        contextMenu.exec(mapToGlobal(pos));
    });
}

3.二级菜单

cpp 复制代码
void MenuAndToolsAndStatus::Init_QSecondary_Menu()
{
    // ----------------------------------------------------------
    // 多层菜单导航栏
    // ----------------------------------------------------------
    QMenuBar* MainMenu = new QMenuBar(this);
    this->setMenuBar(MainMenu);

    // 1.定义父级菜单
    QMenu* EditMenu = MainMenu->addMenu("文件编辑");
    // 1.1 定义 EditMemu 下面的子菜单
    QAction* text = new QAction(EditMenu);
    text->setText("编辑文件"); // 设置文本内容
    text->setShortcut(Qt::CTRL | Qt::Key_A); // 设置快捷键ctrl+a
    EditMenu->addAction(text);
    // 在配置模式与编辑文件之间增加虚线
    EditMenu->addSeparator();
    QAction* option = new QAction(EditMenu);
    option->setText("配置模式");
    EditMenu->addAction(option);

    // 1.1.2 定义Option配置模式下的子菜单
    QMenu* childMenu = new QMenu();

    QAction* set_file = new QAction(childMenu);
    set_file->setText("设置文件内容");
    set_file->setShortcut(Qt::CTRL | Qt::Key_B);
    childMenu->addAction(set_file);

    QAction* read_file = new QAction(childMenu);
    read_file->setText("读取文件内容");
    read_file->setShortcut(Qt::CTRL | Qt::Key_C);
    childMenu->addAction(read_file);

    // ----------------------------------------------------------
    // 注册菜单到窗体中
    // ----------------------------------------------------------
    // 首先将childMenu注册到option中
    option->setMenu(childMenu);
    // 然后再将childMenu加入到EditMenu中
    // EditMenu->addMenu(childMenu);

    // ----------------------------------------------------------
    // 绑定信号和槽
    // ----------------------------------------------------------
    connect(text, &QAction::triggered, this, [=]() {
        QMessageBox::information(nullptr, "提示", "触发编辑文件", QMessageBox::Ok);
    });

    connect(set_file, &QAction::triggered, this, [=]() {
        QMessageBox::information(nullptr, "提示", "触发设置文件", QMessageBox::Ok);
    });

    connect(read_file, &QAction::triggered, this, [=]() {
        QMessageBox::information(nullptr, "提示", "触发读取文件", QMessageBox::Ok);
    });
}

4.状态栏

cpp 复制代码
void MenuAndToolsAndStatus::Init_StatusBar()
{
     QStatusBar* statusbar = new QStatusBar();
     setStatusBar(statusbar);
     QLabel* url = new QLabel(this);
     QLabel* about = new QLabel(this);
     url->setFrameStyle(QFrame::Box | QFrame::Sunken);
     url->setText(tr("<a href=\"https://www.baidu.com\">访问百度</a>"));
     url->setOpenExternalLinks(true);
     about->setFrameStyle(QFrame::Box | QFrame::Sunken);
     about->setText(tr("<a href=\"https://www.taobao.com\">访问淘宝</a>"));
     about->setOpenExternalLinks(true);
     // 将信息增加到底部(永久添加)
     statusbar->addPermanentWidget(url);
     statusbar->addPermanentWidget(about);
     // 隐藏状态栏下方三角形
     statusbar->setSizeGripEnabled(false);
}
相关推荐
lambo mercy3 小时前
Day3:结构化输出
java·数据库·oracle
派小心.3 小时前
小程序分享链路中来源参数的传递校验
大数据·前端·数据库·人工智能·小程序·数据分析
Είναι η κοπέλα3 小时前
向量数据库与嵌入模型:Chroma/FAISS/Milvus 选型
数据库·milvus·faiss
IvorySQL4 小时前
从轮询到等待——PostgreSQL WAIT FOR LSN 特性深度解析
数据库·postgresql
jimy14 小时前
c++的 Type Traits (一)
开发语言·c++
OceanBase数据库官方博客5 小时前
被 VLDB 2027接收OceanBase 最新论文让数据库基(技术解析与实践)
数据库·人工智能·oceanbase
Web极客码5 小时前
Python Ollama 本地 Agent 入门与订单查询实践
开发语言·jvm·python
创邻科技5 小时前
国产图数据库刷新 LDBC 世界纪录:创邻科技 Galaxybase 在 SF100、SF300、SF1000 测试中全面领先”
数据库·科技
kimnoic5 小时前
PHP 8.2的DNF类型怎么用才规范
开发语言·php
GreatSQL社区5 小时前
主从复制SQL线程中断——字符集砸出的坑
java·数据库·算法