Qt学习-16 <QBoxLayout布局>

本文详细介绍了QBoxLayout在Qt中的使用,包括其子类QHBoxLayout和QVBoxLayout的功能,以及布局接口如添加伸缩空间、设置边距、对齐方式和方向等内容。

——  均为自学记录,如有错误请指正


一、简述

QBoxLayout可以在水平方向或者垂直方向上排列控件,由QHBoxLayout、QVBoxLayout所继承。

  • QHBoxLayout:水平布局
  • QVBoxLayout:垂直布局

二、常用接口

例:

QHBoxlayout * pHlayout = new QHBoxLayout();
PHLayout->addStretch();  //伸缩空间
PHLayout->addWidget(控件名);
...
setLayout(PHLayout);
1. setMargin(int)
  • 设置上下左右边距,均相同。
2. setContentsMargins(int left,int top,int right,int bottom)
  • 设置边距,可不同。
3. setContentsMargins(const QMargins&margins)
  • 设置边距,可不同。
4. setSpacing(int)
  • 设置间距
5. addStretch()
  • 添加一个伸缩空间。
  • 在最后一个控件之后添加,所有控件居左;
  • 在第一个控件前添加,所有控件居右;
  • 在最后一个控件之后,第一个控件之前添加,所有控件居中;
  • 在每个控件之间添加,每个控件之间间距相同“均分”。
6. addWidget(QWidget*,int stretch = 0,Qt::Alignment alignment = 0)
  • 添加控件
  • Qt::Alignment 对齐方式,默认居中
  • Qt::Aligneft | Qt::AlignTop 水平居左,垂直居上(参数举例)
7. setDirection(Direction)
  • 设置布局方向
  • Direction可设置为:QBoxLayout::RightToLeft从左到右;QBoxLayout::TopToBottom从上到下
8. setStretchFactor(QWidget*w,int stretch) & setStretch(QLayout*l,int stretch);
  • 设置控件,布局的拉伸系数
if(en) {// 进入全屏模式 qDebug("Entering full screen mode..."); // 检查widget是否已存在 if (!ui->widget_wav) { qWarning("widget_wav is null, cannot enter full screen"); return; } // 创建全屏窗口 fullScreenWidget = new QWidget(nullptr, Qt::Window | Qt::FramelessWindowHint); fullScreenWidget->setStyleSheet("background-color: black;"); fullScreenWidget->setAttribute(Qt::WA_DeleteOnClose); // 设置布局并添加当前页面 QVBoxLayout* layout = new QVBoxLayout(fullScreenWidget); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); // 保存原始父对象和布局信息 originalParent = ui->widget_wav->parentWidget(); originalLayout = qobject_cast<QLayout*>(originalParent->layout()); // 从原父控件中移除当前widget if (originalLayout) { QLayoutItem* item = originalLayout->itemAt(originalLayout->indexOf(ui->widget_wav)); if (item) { originalLayout->removeWidget(ui->widget_wav); } // 如果是栅格布局,保存具体的行列信息 if (QGridLayout* gridLayout = qobject_cast<QGridLayout*>(originalLayout)) { int index = gridLayout->indexOf(ui->widget_wav); if (index >= 0) { gridLayout->getItemPosition(index, &originalRow, &originalColumn, &originalRowSpan, &originalColumnSpan); qDebug() << "Saved grid position - row:" << originalRow << "column:" << originalColumn << "rowSpan:" << originalRowSpan << "columnSpan:" << originalColumnSpan; } } } else { ui->widget_wav->setParent(nullptr); } layout->addWidget(ui->widget_wav); // 显示全屏窗口 fullScreenWidget->showFullScreen(); }else {// 退出全屏模式 qDebug("Exiting full screen mode..."); if (fullScreenWidget) { // 获取全屏窗口中的页面widget QLayout* layout = fullScreenWidget->layout(); if (layout && layout->count() > 0) { QLayoutItem* item = layout->itemAt(0); if (item) { QWidget* pageWidget = item->widget(); if (pageWidget && pageWidget == ui->widget_wav) { // 从全屏布局中移除widget layout->removeWidget(pageWidget); // 将控件放回原位置 if (originalParent) { pageWidget->setParent(originalParent); /*// 如果有原始布局,尝试恢复 if (originalLayout) { // 这里需要根据原始布局类型进行具体恢复 // 例如如果是QVBoxLayout或QHBoxLayout originalLayout->addWidget(pageWidget); } pageWidget->show();*/ // 根据布局类型进行精确恢复 if (originalLayout) { if (QGridLayout* gridLayout = qobject_cast<QGridLayout*>(originalLayout)) { // 恢复栅格布局中的精确位置 gridLayout->addWidget(pageWidget, originalRow, originalColumn, originalRowSpan, originalColumnSpan); } else if (qobject_cast<QVBoxLayout*>(originalLayout) || qobject_cast<QHBoxLayout*>(originalLayout)) { // 恢复盒式布局 originalLayout->addWidget(pageWidget); } } pageWidget->show(); // 强制更新布局以确保界面正确刷新 originalParent->layout()->update(); originalParent->updateGeometry(); // originalParent = nullptr; // originalLayout = nullptr; } else if (ui->page_play) { // 回退到page_play pageWidget->setParent(ui->page_play); pageWidget->show(); } } } } // 关闭并删除全屏容器 fullScreenWidget->close(); fullScreenWidget->deleteLater();回到原理位置后,布局还是不对,其他控件的布局被改变了
最新发布
11-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值