当前位置:   article > 正文

QT菜单栏和工具栏_qt 菜单与工具栏设计

qt 菜单与工具栏设计

1、菜单栏

  1. 菜单栏最多只有一个
  2. 创建菜单栏:QMenuBar * bar = menuBar();
  3. 将菜单栏放入窗口:setMenuBar(bar);
  4. 创建菜单:QMenu * fileMenu = bar->addMenu("文件"); QMenu * editMenu = bar->addMenu("编辑");
  5. 创建菜单项:在这里插入代码片QAction *newAction = fileMenu->addAction("新建");QAction *openAction = fileMenu->addAction("打开");
  6. 添加分隔线:fileMenu->addSeparator();

2、工具栏

  1. 可以有多个
  2. 创建工具栏:QToolBar * toolBar = new QToolBar(this);
  3. 将工具栏放入窗口,并设置停靠:addToolBar(Qt::LeftToolBarArea,toolBar);
  4. 设置只允许左右停靠:toolBar->setAllowedAreas(Qt::LeftToolBarArea|Qt::RightToolBarArea);
  5. 设置浮动:toolBar->setFloatable(false);
  6. 设置移动:总开关:toolBar->setMovable(true);
  7. 工具栏设置内容:toolBar->addAction(newAction); toolBar->addAction(openAction);
  8. 添加分割线:toolBar->addSeparator();
  9. 工具栏中添加控件:QPushButton * btn = new QPushButton("开始",this); toolBar->addWidget(btn);

3、代码示例

  • mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:

};
#endif // MAINWINDOW_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • mainwindow.cpp
#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //重置窗口大小
    resize(600,400);

    //菜单栏最多只能有一个
    //创建菜单栏
    QMenuBar * bar = menuBar();
    //将菜单栏放入窗口
    setMenuBar(bar);
    //创建菜单
    QMenu * fileMenu = bar->addMenu("文件");
    QMenu * editMenu = bar->addMenu("编辑");
    //创建菜单项
    QAction *newAction = fileMenu->addAction("新建");
    //添加分隔线
    fileMenu->addSeparator();
    QAction *openAction = fileMenu->addAction("打开");

    //工具栏 可以有多个
    //创建工具栏
    QToolBar * toolBar = new QToolBar(this);
    //将工具栏放入窗口,并设置停靠
    addToolBar(Qt::LeftToolBarArea,toolBar);

    //设置只允许左右停靠
    toolBar->setAllowedAreas(Qt::LeftToolBarArea|Qt::RightToolBarArea);
    //设置浮动
    toolBar->setFloatable(false);
    //设置移动:总开关
    toolBar->setMovable(true);

    //工具栏设置内容
    toolBar->addAction(newAction);
    //添加分割线
    toolBar->addSeparator();
    toolBar->addAction(openAction);

    //工具栏中添加控件
    QPushButton * btn = new QPushButton("开始",this);
    toolBar->addWidget(btn);
}

MainWindow::~MainWindow()
{
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • main.cpp
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4、运行结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/962470
推荐阅读
相关标签
  

闽ICP备14008679号