17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)

⼯具栏

⼯具栏是应⽤程序中集成各种功能实现快捷键使⽤的⼀个区域。可以有多个,也可以没有,它并不是应⽤程序中必须存在的组件。它是⼀个可移动的组件,它的元素可以是各种窗⼝组件,它的元素通常以图标按钮的⽅式存在。如下图为⼯具栏的⽰意图:

创建⼯具栏

调⽤QMainWindow类的addToolBar()函数来创建⼯具栏,每增加⼀个⼯具栏都需要调⽤⼀次该函数。

如添加两个⼯具栏:

c++ 复制代码
#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("保存");
    toolBar->addAction(action1);
    QAction* action2 = new QAction("打开");
    toolBar->addAction(action2);
  
    connect(action1, &QAction::triggered, this, &MainWindow::handle1);
    connect(action2, &QAction::triggered, this, &MainWindow::handle2);
}
  
MainWindow::~MainWindow()
{
    delete ui;
}
  
void MainWindow::handle1()
{
    qDebug() << "handle1";
}
  
void MainWindow::handle2()
{
    qDebug() << "handle2";
}

使用QToolBar表示工具栏对象

一个窗口可以有多个工具栏,也可以没有

工具栏往往也可以手动移动位置

添加菜单栏,使用的是setMenuBar

添加工具栏,使用的是addToolBar

菜单栏只能有一个.重复设置,新的替换旧的(set就包含了"替换")

工具栏,可以有多个.重复设置,就会出现多个工具栏不包含"替换"

c++ 复制代码
    action1->setIcon(QIcon(":/save.png"));
    action2->setIcon(QIcon(":/new.png"));

QAction如果出现在工具栏上,也会产生图标覆盖文本这样的情况

会以toolTip的方式来存在

鼠标悬停上去的时候,就会显示出一段提示信息

另外也可以手动设置这里的toolTip

c++ 复制代码
    action1->setToolTip("点击这里保存文件");
c++ 复制代码
#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("保存");
    QAction* action2 = new QAction("打开");
    action1->setToolTip("点击这里保存文件");
    action1->setIcon(QIcon(":/save.png"));
    action2->setIcon(QIcon(":/new.png"));
  
    //菜单项放在工具栏中
    toolBar->addAction(action1);
    toolBar->addAction(action2);
    //菜单项放在菜单中
    menu->addAction(action1);
    menu->addAction(action2);
  
    connect(action1, &QAction::triggered, this, &MainWindow::handle1);
    connect(action2, &QAction::triggered, this, &MainWindow::handle2);
}
  
MainWindow::~MainWindow()
{
    delete ui;
}
  
void MainWindow::handle1()
{
    qDebug() << "handle1";
}
  
void MainWindow::handle2()
{
    qDebug() << "handle2";
}

工具栏往往也是和菜单栏搭配使用的

工具栏中的QAction也可以出现在菜单中

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

只会释放一次,不会重复delete

设置停靠位置

⼯具栏停靠位置的设置有两种⽅式。⼀种是在创建⼯具栏的同时指定停靠的位置,另⼀种是通过QToolBar类提供的setAllowedAreas()函数来设置。

⽅式⼀:创建⼯具栏的同时指定其停靠的位置。

在创建⼯具栏的同时,也可以设置⼯具栏的位置,其默认位置是在窗⼝的最上⾯;如上述代码,默认在最上⾯显⽰。⼯具栏允许停靠的区域由QToolBar类提供的allowAreas()函数决定,其中可以设置的位置包括:

  1. 可以设置工具栏出现的初始位置(上下左右...)
  2. 可以设置工具栏允许停放到哪些边缘
  3. 可以设置工具栏是否允许浮动
  4. 可以设置工具栏是否可以移动
  • Qt::LeftToolBarArea:停靠在左侧
  • Qt::RightToolBarArea:停靠在右侧
  • Qt::TopToolBarArea:停靠在顶部
  • Qt::BottomToolBarArea:停靠在底部
  • Qt::AllToolBarAreas:以上四个位置都可停靠
c++ 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  
    QToolBar* toolBar1 = new QToolBar();
    QToolBar* toolBar2 = new QToolBar();
  
    this->addToolBar(toolBar1);
    this->addToolBar(toolBar2);
  
    QAction* action1 = new QAction("动作1");
    QAction* action2 = new QAction("动作2");
    QAction* action3 = new QAction("动作3");
    QAction* action4 = new QAction("动作4");
  
    toolBar1->addAction(action1);
    toolBar1->addAction(action2);
    toolBar2->addAction(action3);
    toolBar2->addAction(action4);
}
c++ 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>
  
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QToolBar* toolBar1 = new QToolBar();
    QToolBar* toolBar2 = new QToolBar();
  
    this->addToolBar(toolBar1);
    this->addToolBar(Qt::LeftToolBarArea, toolBar2);
  
    QAction* action1 = new QAction("动作1");
    QAction* action2 = new QAction("动作2");
    QAction* action3 = new QAction("动作3");
    QAction* action4 = new QAction("动作4");
  
    toolBar1->addAction(action1);
    toolBar1->addAction(action2);
    toolBar2->addAction(action3);
    toolBar2->addAction(action4);
}

⽅式⼆:使⽤QToolBar类提供的setAllowedAreas()函数设置停靠位置。如下⽰例:

c++ 复制代码
    QToolBar* toolBar1 = new QToolBar();
    QToolBar* toolBar2 = new QToolBar();
  
    this->addToolBar(toolBar1);
    this->addToolBar(Qt::LeftToolBarArea, toolBar2);

	//允许在左右侧停靠
    toolBar2->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);

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

设置浮动属性

⼯具栏的浮动属性可以通过 QToolBar类 提供的setFloatable()函数 来设置。setFloatable()函数原型为:

void setFloatable(bool floatable)

参数:

true:浮动

false:不浮动

c++ 复制代码
    QToolBar* toolBar1 = new QToolBar();
    QToolBar* toolBar2 = new QToolBar();
  
    this->addToolBar(toolBar1);
    this->addToolBar(Qt::LeftToolBarArea, toolBar2);
  
    //只允许停靠在左侧或者右侧
    toolBar2->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);
    //设置不允许浮动
    toolBar2->setFloatable(false);
设置移动属性

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

void setMovable(bool movable)

参数:

true:移动

false:不移动

说明:若设置⼯具栏为不移动状态,则设置其停靠位置的操作就不会⽣效,所以设置⼯具栏的移动属性类似于总开关的效果

c++ 复制代码
    QToolBar* toolBar1 = new QToolBar();
    QToolBar* toolBar2 = new QToolBar();
  
    this->addToolBar(toolBar1);
    this->addToolBar(Qt::LeftToolBarArea, toolBar2);
  
    //只允许停靠在左侧或者右侧
    toolBar2->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);
    //设置不允许浮动
    toolBar2->setFloatable(false);
    //设置不允许移动
    toolBar2->setMovable(false);
综合⽰例

状态栏

状态栏是应⽤程序中输出简要信息的区域。⼀般位于主窗⼝的最底部,⼀个窗⼝中最多只能有⼀个状态栏。在Qt中,状态栏是通过QStatusBar类来实现的。在状态栏中可以显⽰的消息类型有:

  • 实时消息:如当前程序状态
  • 永久消息:如程序版本号,机构名称
  • 进度消息:如进度条提⽰,百分百提⽰
状态栏的创建

状态栏的创建是通过QMainWindow类提供的statusBar()函数来创建;⽰例如下:

c++ 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
  
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  
    //存在就获取,不存在就创建
    QStatusBar* statusBar = this->statusBar();
    //如果状态栏没有被创建,这样的设置是必要的
    //如果状态栏已经在窗口中存在,这样设置的意义不大,但也没有副作用,仍然保留
    this->setStatusBar(statusBar);
}
在状态栏中显⽰实时消息

在状态栏中显⽰实时消息是通过showMessage()函数来实现,⽰例如下:

c++ 复制代码
    //显示一个临时的信息
    statusBar->showMessage("这是一个状态消息", 3000);

3秒之后,消息自动消失

通过showMessage可以在状态栏中显示一个文本.

此时这个文本存在的时间可以自定义.

timeout参数是一个单位为ms的时间,如果timeout为0(不填),消息就会持久存在

在状态栏中显⽰永久消息

在状态栏中可以显⽰永久消息,此处的永久消息是通过标签来显⽰的;⽰例如下:

addWidget从左往右添加.

addPermanentWidget从右往左添加.

c++ 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
  
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  
    //存在就获取,不存在就创建
    QStatusBar* statusBar = this->statusBar();
    //如果状态栏没有被创建,这样的设置是必要的
    //如果状态栏已经在窗口中存在,这样设置的意义不大,但也没有副作用,仍然保留
    this->setStatusBar(statusBar);
  
    //给状态栏添加子控件
    QLabel* label = new QLabel("这是一个QLabel");
    statusBar->addWidget(label);
}
c++ 复制代码
    //给状态栏添加子控件
    QLabel* label = new QLabel("这是一个QLabel");
    statusBar->addWidget(label);
  
    QLabel* label2 = new QLabel("这是另一个QLabel");
    statusBar->addPermanentWidget(label2);
拉伸系数
c++ 复制代码
    //给状态栏添加子控件
    QLabel* label = new QLabel("这是一个QLabel");
    statusBar->addWidget(label, 1);
  
    QLabel* label2 = new QLabel("这是另一个QLabel");
    statusBar->addWidget(label2, 2);
添加进度条
c++ 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QProgressBar>
  
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  
    //存在就获取,不存在就创建
    QStatusBar* statusBar = this->statusBar();
    //如果状态栏没有被创建,这样的设置是必要的
    //如果状态栏已经在窗口中存在,这样设置的意义不大,但也没有副作用,仍然保留
    this->setStatusBar(statusBar);
  
    //给状态栏添加子控件
    QLabel* label = new QLabel("这是一个QLabel");
    statusBar->addWidget(label);
  
    QProgressBar* progressBar = new QProgressBar();
    progressBar->setRange(0, 100);
    progressBar->setValue(50);
    statusBar->addWidget(progressBar);
}
添加按钮
c++ 复制代码
    QPushButton* button = new QPushButton("按钮");
    statusBar->addPermanentWidget(button);

浮动窗⼝

在Qt中,浮动窗⼝也称之为铆接部件。浮动窗⼝是通过QDockWidget类来实现浮动的功能。浮动窗⼝⼀般是位于核⼼部件的周围,可以有多个。

浮动窗⼝的创建

浮动窗⼝的创建是通过QDockWidget类提供的构造⽅法QDockWidget()函数动态创建的;⽰例如下:

c++ 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDockWidget>
  
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  
    //给主窗口添加一个子窗口
    QDockWidget* dockWidget = new QDockWidget();
    //使用addDockWidget把浮动窗口加入到子窗口中
    this->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
}
c++ 复制代码
    //设置窗口标题
    dockWidget->setWindowTitle("这是一个浮动窗口");
c++ 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDockWidget>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
  
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  
    //给主窗口添加一个子窗口
    QDockWidget* dockWidget = new QDockWidget();
    //使用addDockWidget把浮动窗口加入到子窗口中
    this->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
  
    //设置窗口标题
    dockWidget->setWindowTitle("这是一个浮动窗口");
  
    //给浮动窗口内部,添加一些其他控件
    //不能直接给浮动窗口添加子控件,而是要单独创建一个QWidget,把要添加的控件加入到QWidget中
    //然后再把这个QWidget设置到dockWidget中
    QWidget* container = new QWidget();
    dockWidget->setWidget(container);
  
    //创建布局管理器,把布局管理器设置到QWidget中
    QVBoxLayout* layout = new QVBoxLayout;
    container->setLayout(layout);
    //创建其他控件
    QLabel* label = new QLabel("这是一个QLabel");
    QPushButton* button = new QPushButton("这是按钮");
    layout->addWidget(label);
    layout->addWidget(button);
}
c++ 复制代码
    //设置浮动窗口允许停靠位置
    dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::TopDockWidgetArea);
相关推荐
hjjdebug30 分钟前
constexpr 关键字的意义(入门)
c++·constexpr
虾球xz2 小时前
游戏引擎学习第282天:Z轴移动与摄像机运动
c++·学习·游戏引擎
.小墨迹2 小时前
Apollo学习——planning模块(3)之planning_base
linux·开发语言·c++·学习·自动驾驶
龙湾开发2 小时前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 10.增强表面细节(一)过程式凹凸贴图
c++·笔记·学习·3d·图形渲染
德亦周2 小时前
如何在Mac电脑上的VScode去配置C/C++环境
c++·vscode·macos
XiaoyaoCarter3 小时前
每日一道leetcode(新学数据结构版)
数据结构·c++·算法·leetcode·职场和发展·哈希算法·前缀树
八月的雨季 最後的冰吻3 小时前
SIP协议栈--osip源码梳理
linux·服务器·网络·c++·网络协议
charlie1145141913 小时前
基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
arm开发·qt·学习·pdf·教程·设计·qt6
fancy1661664 小时前
搜索二维矩阵 II
c++·算法·矩阵