C++客户端Qt开发——Qt窗口(工具栏)

2.工具栏

使用QToolBar表示工具栏对象,一个窗口可以有多个工具栏,也可以没有,工具栏往往也可以手动移动位置

①设置工具栏

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QToolBar>
#include<QDebug>

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

    QToolBar* toolbar = new QToolBar();

    this->addToolBar(toolbar);

    QAction* action1 = new QAction("保存");
    action1->setIcon(QIcon(":/1.jpg"));
    QAction* action2 = new QAction("打开");
    action2->setIcon(QIcon(":/2.jpg"));

    toolbar->addAction(action1);
    toolbar->addAction(action2);

    connect(action1,&QAction::triggered,this,&MainWindow::handle);
    connect(action2,&QAction::triggered,this,&MainWindow::close);

}

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

void MainWindow::handle()
{
    qDebug()<<"触发保存";
}

void MainWindow::close()
{
    qDebug()<<"触发打开";
}

②工具栏中的action也能在菜单栏中显示出来

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QToolBar>
#include<QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建菜单栏
    QMenuBar* menubar = this->menuBar();
    this->setMenuBar(menubar);
    //创建菜单
    QMenu* menu = new QMenu("文件");
    menubar->addMenu(menu);



    //创建工具栏
    QToolBar* toolbar = new QToolBar();

    this->addToolBar(toolbar);

    QAction* action1 = new QAction("保存");
    action1->setToolTip("点击这里保存文件");
    action1->setIcon(QIcon(":/1.jpg"));
    QAction* action2 = new QAction("打开");
    action2->setIcon(QIcon(":/2.jpg"));
    //菜单项设置到菜单中
    menu->addAction(action1);
    menu->addAction(action2);
    //菜单项设置到工具栏中
    toolbar->addAction(action1);
    toolbar->addAction(action2);

    connect(action1,&QAction::triggered,this,&MainWindow::handle);
    connect(action2,&QAction::triggered,this,&MainWindow::close);

}

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

void MainWindow::handle()
{
    qDebug()<<"触发保存";
}

void MainWindow::close()
{
    qDebug()<<"触发打开";
}

如果一个QAction既是QMenu的子元素,又是QToolBar的子元素,释放的时候,是否会重复delete?

不会,qt的对象树机制处理好了这样的释放

③设置多个工具栏

工具栏允许停靠的区域由QToolBar类提供的allowAreas()函数决定,其中可以设置的位置包括:

  • Qt:LeftToolBarArea:停靠在左侧
  • Qt:RightToolBarArea:停靠在右侧
  • Qt:TopToolBarArea:停靠在顶部
  • Qt:BottomToolBarArea:停靠在底部
  • Qt::AllToolBarAreas:以上四个位置都可停靠

还可以使用QToolBar类提供的setAllowedAreas()函数设置停靠位置。

  • 只允许在左侧停靠:toolBar1->setAllowedAreas(Qt:LeftToolBarArea);
  • 只允许在右侧停靠:toolBar2->setAllowedAreas(Qt:RightToolBarArea);

设置工具栏的移动属性可以通过QToolBar类提供的setMovable()函数来设置。

  • 设置不允许浮动:toolBar2->setFloatable(false);
  • 设置不允许移动:toolBar2->setMovable(false);

说明:

在创建工具栏的同时指定其停靠的位置,指的是程序运行时工具栏默认所在的位置;而使用setAllowedAreas()函数设置停靠位置,指的是工具栏允许其所能停靠的位置。

相关推荐
anlogic27 分钟前
Java基础 8.18
java·开发语言
沐知全栈开发1 小时前
WebForms XML 文件详解
开发语言
scx201310041 小时前
20250814 最小生成树和重构树总结
c++·算法·最小生成树·重构树
阿巴~阿巴~2 小时前
冒泡排序算法
c语言·开发语言·算法·排序算法
看到我,请让我去学习3 小时前
QT - QT开发进阶合集
开发语言·qt
weixin_307779133 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
励志不掉头发的内向程序员4 小时前
STL库——string(类函数学习)
开发语言·c++
一百天成为python专家5 小时前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
Sunhen_Qiletian5 小时前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
hqwest5 小时前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局