当前位置: 首页 > news >正文

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

⼯具栏

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

创建⼯具栏

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

#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就包含了"替换")
工具栏,可以有多个.重复设置,就会出现多个工具栏不包含“替换”
![[Pasted image 20250423124419.png]]

    action1->setIcon(QIcon(":/save.png"));action2->setIcon(QIcon(":/new.png"));

QAction如果出现在工具栏上,也会产生图标覆盖文本这样的情况
会以toolTip的方式来存在
鼠标悬停上去的时候,就会显示出一段提示信息
另外也可以手动设置这里的toolTip
![[Pasted image 20250423124838.png]]

    action1->setToolTip("点击这里保存文件");

![[Pasted image 20250423125513.png]]

#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";
}

![[Pasted image 20250423130419.png]]

工具栏往往也是和菜单栏搭配使用的
工具栏中的QAction也可以出现在菜单中
如果一个QAction既是QMenu的子元素,又是QToolBar的子元素,释放的时候,是否会重复delete
只会释放一次,不会重复delete

设置停靠位置

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

  1. 可以设置工具栏出现的初始位置(上下左右…)
  2. 可以设置工具栏允许停放到哪些边缘
  3. 可以设置工具栏是否允许浮动
  4. 可以设置工具栏是否可以移动
  • Qt::LeftToolBarArea:停靠在左侧
  • Qt::RightToolBarArea:停靠在右侧
  • Qt::TopToolBarArea:停靠在顶部
  • Qt::BottomToolBarArea:停靠在底部
  • Qt::AllToolBarAreas:以上四个位置都可停靠
#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);
}

![[Pasted image 20250423132806.png]]

#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);
}

![[Pasted image 20250423134326.png]]

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

    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:不浮动

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

    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);

![[Pasted image 20250423135906.png]]

综合⽰例

![[Pasted image 20250423140033.png]]

状态栏

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

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

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

#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()函数来实现,⽰例如下:

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

![[Pasted image 20250423140917.png]]

3秒之后,消息自动消失
通过showMessage可以在状态栏中显示一个文本.
此时这个文本存在的时间可以自定义.
timeout参数是一个单位为ms的时间,如果timeout为0(不填),消息就会持久存在

在状态栏中显⽰永久消息

在状态栏中可以显⽰永久消息,此处的永久消息是通过标签来显⽰的;⽰例如下:
![[Pasted image 20250423141848.png]]

addWidget从左往右添加.
addPermanentWidget从右往左添加.

#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);
}

![[Pasted image 20250423142045.png]]

    //给状态栏添加子控件QLabel* label = new QLabel("这是一个QLabel");statusBar->addWidget(label);QLabel* label2 = new QLabel("这是另一个QLabel");statusBar->addPermanentWidget(label2);

![[Pasted image 20250423142229.png]]

拉伸系数
    //给状态栏添加子控件QLabel* label = new QLabel("这是一个QLabel");statusBar->addWidget(label, 1);QLabel* label2 = new QLabel("这是另一个QLabel");statusBar->addWidget(label2, 2);

![[Pasted image 20250423142405.png]]

添加进度条
#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);
}

![[Pasted image 20250423142642.png]]

添加按钮
    QPushButton* button = new QPushButton("按钮");statusBar->addPermanentWidget(button);

![[Pasted image 20250423142822.png]]

浮动窗⼝

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

浮动窗⼝的创建

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

#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);
}

![[Pasted image 20250423143822.png]]

    //设置窗口标题dockWidget->setWindowTitle("这是一个浮动窗口");

![[Pasted image 20250423144031.png]]

#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);
}

![[Pasted image 20250423144803.png]]

    //设置浮动窗口允许停靠位置dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::TopDockWidgetArea);

相关文章:

  • 软件黑盒与白盒测试详解
  • 大厂Java面试:JVM调优与问题定位
  • 我的独立开发技术栈
  • Kotlin中实现静态
  • 深入解析C++ STL Queue:先进先出的数据结构
  • IMU---MPU6050
  • 数据结构-链表
  • 基于stm32的智能门锁系统
  • “时间”,在数据处理中的真身——弼马温一般『无所不能』(DeepSeek)
  • 明远智睿2351开发板:性价比之选,赋能智能硬件创新
  • 【测试报告】幸运闪烁抽奖系统(Java+Selenium+Jmeter自动化测试)
  • cJSON
  • Linux中进程的属性:状态
  • 18487.1-2015-解读笔记五-交流充电之停止充电
  • AI与Web3.0:技术融合
  • C#语言实现PDF转Excel
  • 26、C# 中是否可以继承String类?为什么?
  • GD32F407单片机开发入门(六)定时器TIMER详解及实战含源码
  • Redis 服务自动开启、设置密码和闪退问题
  • .NET、java、python语言连接SAP系统的方法
  • 沂水县委书记陈士贤,跨市履新泰安市委常委、组织部部长
  • 2024年我国数字阅读用户规模达6.7亿
  • 神二十6个半小时到站
  • 上海消保委调查二次元消费:手办与卡牌受欢迎,悦己和社交是动力
  • 十二届上海市委第六轮巡视启动,对18家市管单位开展常规巡视
  • 受贿超8.22亿,新疆维吾尔自治区党委原副书记李鹏新一审被判死缓