QT中布局器的addStretch函数使用效果

分手后的思念是犯贱 2022-12-24 01:51 60阅读 0赞

QBoxLayout中addStretch

函数说明:

void QBoxLayout::addStretch(int stretch = 0)

Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.

函数的作用是在布局器中增加一个伸缩量,里面的参数表示QSpacerItem的个数,默认值为零,会将你放在layout中的空间压缩成默认的大小。

例如:一个layout布局器,里面有三个控件,一个放在最左边,一个放在最右边,最后一个放在layout的1/3处,这就可以通过addStretch去实现。
例:用addStretch函数实现将nLayout的布局器的空白空间平均分配

  1. QHBoxLayout *buttonLayout = new QHBoxLayout;
  2. QPushButton *button1;
  3. QPushButton *button2;
  4. QPushButton *button3;
  5. button1 = new QPushButton;
  6. button2 = new QPushButton;
  7. button3 = new QPushButton;
  8. buttonLayout->addStretch(1);
  9. buttonLayout->addWidget(button1);
  10. buttonLayout->addStretch(1);
  11. buttonLayout->addWidget(button2);
  12. buttonLayout->addStretch(1);
  13. buttonLayout->addWidget(button3);
  14. buttonLayout->addStretch(6);
  15. buttonLayout->setContentsMargins(0, 0, 0, 0);

其中四个addStretch()函数用于在button按钮间增加伸缩量,伸缩量的比例为1:1:1:6,意思就是将button以外的空白地方按设定的比例等分为9份并按照设定的顺序放入buttonLayout布局器中。
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,60人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Qt:在Qt Designer使用布局

    在使用窗体之前,需要将窗体上的对象放置到布局中。这样可以确保在预览窗体或在应用程序中使用窗体时,对象将正确显示。在布局中放置对象还可以确保在调整窗体大小时正确调整对象的大小。

    相关 Qt布局管理

    布局管理器可以随着窗口大小动态的变更布局控件的位置和大小 1、QHBoxLayout水平控件布局 ![70][] ![70 1][] 2、QVBoxLayout垂直控件

    相关 Qt学习笔记-布局管理

    在设计较复杂的GUI用户界面时,仅通过指定窗口部件的父子关系以期达到加载和排列窗口部件的方法是行不通的,最好的办法是使用Qt提供的布局管理器。 QGridLayout \ma