qt menubar toolbar statusbar 使用

参考 https://cloud.tencent.com/developer/article/2371577
参考 https://blog.csdn.net/weixin_43212394/article/details/107941506
menubar 图片和文字无法同步共存的解决方案 https://jishurizhi.com/p-173.html
menubar 图片和文字无法同步共存的解决方案 https://blog.csdn.net/weixin_43212394/article/details/107941506

    // qt menubar toolbar  statusbar
    if(1){
        // 菜单
        QMenu* parentMenu=new QMenu("父菜单");
        QMenu* childMenu=new QMenu("子菜单");

        ui->menubar->addMenu(parentMenu);
        parentMenu->addMenu(childMenu);
        childMenu->setIcon(QIcon(":/img/icon/add_no_img.png"));// 父类不行,只能显示 icon或者文字,不允许同时显示.

        QAction* action1 =new QAction("子菜单项1");
        QAction* action2 =new QAction("子菜单项2");
        QAction* action3 =new QAction("子菜单项3");

        childMenu->addAction(action1);
        childMenu->addAction(action2);
        childMenu->addAction(action3);

        QMenu* parentMenu2=new QMenu("菜单");
        ui->menubar->addMenu(parentMenu2);


        // 隐藏菜单栏上的右击菜单
        this->setContextMenuPolicy(Qt::NoContextMenu);
        //ui->menubar->setVisible(false);// 隐藏菜单

        // ----------------------------------------------------------
        // 创建menuBar组件
        // ----------------------------------------------------------
        // 创建基础顶部菜单并让其隐藏
        QMenuBar *bar = menuBar();
        this->setMenuBar(bar);
        QMenu * fileMenu = bar->addMenu("default_menu");
        fileMenu->menuAction()->setVisible(false);// 设置不可见

        // 添加子菜单
        QAction *NewAction = fileMenu->addAction("新建文件");
        QAction *OpenAction = fileMenu->addAction("打开文件");
        QAction *ReadAction = fileMenu->addAction("读入文件");

        // 分别设置图标
        NewAction->setIcon(QIcon(":/img/icon/add_no_img.png"));
        OpenAction->setIcon(QIcon(":/img/icon/add_no_img.png"));
        ReadAction->setIcon(QIcon(":/img/icon/add_no_img.png"));


        //QToolBar *toolBar = new QToolBar(this);
        QToolBar *toolBar = ui->toolBar;
        addToolBar(Qt::TopToolBarArea,toolBar);

        // 将菜单项依次添加到工具栏
        toolBar->addAction(NewAction);
        toolBar->addAction(OpenAction);
        toolBar->addAction(ReadAction);

        // 设置禁止移动属性,工具栏默认贴在上方
        toolBar->setFloatable(false);
        toolBar->setMovable(false);
        toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

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

        connect(OpenAction,&QAction::triggered,this,[=](){
            QMessageBox::information(nullptr,"提示","触发打开文件按钮",QMessageBox::Ok);
        });

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

    if(1){
        // 初始化状态栏
           QLabel *labCellIndex = new QLabel("当前坐标: 0.0",this);
           labCellIndex->setMinimumWidth(150);

           QLabel *labCellType=new QLabel("单元格类型: null",this);
           labCellType->setMinimumWidth(100);

           QLabel *labStudID=new QLabel("学生ID: 0",this);
           labStudID->setMinimumWidth(100);

           // 将初始化的标签添加到底部状态栏上
           ui->statusbar->addWidget(labCellIndex);
           ui->statusbar->addWidget(labCellType);
           ui->statusbar->addWidget(labStudID);
    }

posted @ 2025-04-23 11:46  小城熊儿  阅读(12)  评论(0)    收藏  举报