QGridLayout 详解



一、QGridLayout属性介绍

 

1、QGridlayout以方格的形式管理窗口部件,先看QGridLayout的属性,如下图

2、各个参数的介绍

layoutLeftMargin ...至layoutBottomMargin在ui_MainWindow.h中自动生成的代码是:

gridLayout->setContentsMargins(20, 10, 10, 10);

学过CSS都知道,这是设置一个元素所有外边距的宽度,或者设置各边上外边距的宽度

On most platforms, the margin is 11 pixels in all directions.

 

HorizontalSpacing...至VerticalSpacing在ui_MainWindow.h中自动生成的代码是:

 

        gridLayout->setHorizontalSpacing(6);

        gridLayout->setVerticalSpacing(6);

        这是设置两个控件之间的水平和竖直距离

 

LayoutRowStretch在ui_MainWindow.h中自动生成的代码是:

 

gridLayout->setRowStretch(0, 1);

        gridLayout->setRowStretch(1, 1);

        gridLayout->setRowStretch(2, 1);

表示在第0行、第1行、第2行 在竖直方向的空间比例分配,大家稍微改一下参数就能看出来效果

LayoutColumnStretch在ui_MainWindow.h中自动生成的代码是:      

gridLayout->setColumnStretch(1, 1);

 

表示设置第0列、第1列两者在水平方向的空间比例分配。

 

 

LayoutRowMinimumHeight在ui_MainWindow.h中自动生成的代码是:

 

        gridLayout->setRowMinimumHeight(0, 1);

        gridLayout->setRowMinimumHeight(1, 2);

        gridLayout->setRowMinimumHeight(2, 3);

表示在第0行、第1行、第2行的最小高度是1pixels,2pixels,3pixels

LayoutColumnMinimumWidth在ui_MainWindow.h中自动生成的代码是:

 

  gridLayout->setColumnMinimumWidth(0, 4);

        gridLayout->setColumnMinimumWidth(1, 5);

表示设置第0列、第1列的最小宽度是4pixels、5pixels

 

 

LayoutSizeConstraint在ui_MainWindow.h中自动生成的代码是:

gridLayout->setSizeConstraint(QLayout::SetDefaultConstraint);

 

This property holds the resize mode of the layout.看下表

 

 

 

 

enum QLayout::SizeConstraint

The possible values are:

Constant Value Description
QLayout::SetDefaultConstraint0The main widget's minimum size is set to minimumSize(), unless the widget already has a minimum size.
QLayout::SetFixedSize3The main widget's size is set to sizeHint(); it cannot be resized at all.
QLayout::SetMinimumSize2The main widget's minimum size is set to minimumSize(); it cannot be smaller.
QLayout::SetMaximumSize4The main widget's maximum size is set to maximumSize(); it cannot be larger.
QLayout::SetMinAndMaxSize5The main widget's minimum size is set to minimumSize() and its maximum size is set tomaximumSize().
QLayout::SetNoConstraint1

The widget is not constrained.

 

 

 

QFormLayout属性介绍

1、QFormLayout类管理输入型控件和它的label组成的那些form表格,包括它的界面参数如下图

 

 

2、界面中对应的代码如下表,

 

 

Cpp代码   收藏代码
  1. formLayout = new QFormLayout(widget1);  
  2.         formLayout->setSpacing(6);  
  3.         formLayout->setContentsMargins(11, 11, 11, 11);  
  4.         formLayout->setObjectName(QString::fromUtf8("formLayout"));  
  5.         formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);  
  6.         formLayout->setRowWrapPolicy(QFormLayout::DontWrapRows);  
  7.         formLayout->setContentsMargins(0, 0, 0, 0);  
  8.         label_4 = new QLabel(widget1);  
  9.         label_4->setObjectName(QString::fromUtf8("label_4"));  
  10.   
  11.         formLayout->setWidget(0, QFormLayout::LabelRole, label_4);  
  12.   
  13.         lineEdit = new QLineEdit(widget1);  
  14.         lineEdit->setObjectName(QString::fromUtf8("lineEdit"));  
  15.   
  16.         formLayout->setWidget(0, QFormLayout::FieldRole, lineEdit);  
  17.   
  18.         label_5 = new QLabel(widget1);  
  19.         label_5->setObjectName(QString::fromUtf8("label_5"));  
  20.   
  21.         formLayout->setWidget(1, QFormLayout::LabelRole, label_5);  
  22.   
  23.         comboBox = new QComboBox(widget1);  
  24.         comboBox->setObjectName(QString::fromUtf8("comboBox"));  
  25.   
  26.         formLayout->setWidget(1, QFormLayout::FieldRole, comboBox);  

 

3、其中值得一说的是:LayoutFieldGrowthPolicy属性

 

enum QFormLayout::FieldGrowthPolicy

This enum specifies the different policies that can be used to control the way in which the form's fields grow.

Constant Value Description
QFormLayout::FieldsStayAtSizeHint0The fields never grow beyond their effective size hint. This is the default forQMacStyle.
QFormLayout::ExpandingFieldsGrow1Fields with an horizontal size policy of Expanding or MinimumExpanding will grow to fill the available space. The other fields will not grow beyond their effective size hint. This is the default policy for Plastique.
QFormLayout::AllNonFixedFieldsGrow2All fields with a size policy that allows them to grow will grow to fill the available space. This is the default policy for most styles.

 

 

 

4、还有一个属性值得说:LayoutRowWrapPolicy

 

This property holds the way in which the form's rows wrap.

//这个属性设置了表格如何排版各个元素

If you want to display each label above its associated field (instead of next to it), set this property to WrapAllRows.

//如果你想把每个标签放在相关字段的上方,而不是和它相邻,就设置这个属性值为WrapAllRows。

 

enum QFormLayout::RowWrapPolicy

This enum specifies the different policies that can be used to control the way in which the form's rows wrap.

Constant Value Description
QFormLayout::DontWrapRows0Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles and QS60Style.
QFormLayout::WrapLongRows1Labels are given enough horizontal space to fit the widest label, and the rest of the space is given to the fields. If the minimum size of a field pair is wider than the available space, the field is wrapped to the next line. This is the default policy for Qt Extended styles and andQS60Style.
QFormLayout::WrapAllRows2Fields are always laid out below their label.

一、QGridLayout属性介绍

 

1、QGridlayout以方格的形式管理窗口部件,先看QGridLayout的属性,如下图

2、各个参数的介绍

layoutLeftMargin ...至layoutBottomMargin在ui_MainWindow.h中自动生成的代码是:

gridLayout->setContentsMargins(20, 10, 10, 10);

学过CSS都知道,这是设置一个元素所有外边距的宽度,或者设置各边上外边距的宽度

On most platforms, the margin is 11 pixels in all directions.

 

HorizontalSpacing...至VerticalSpacing在ui_MainWindow.h中自动生成的代码是:

 

        gridLayout->setHorizontalSpacing(6);

        gridLayout->setVerticalSpacing(6);

        这是设置两个控件之间的水平和竖直距离

 

LayoutRowStretch在ui_MainWindow.h中自动生成的代码是:

 

gridLayout->setRowStretch(0, 1);

        gridLayout->setRowStretch(1, 1);

        gridLayout->setRowStretch(2, 1);

表示在第0行、第1行、第2行 在竖直方向的空间比例分配,大家稍微改一下参数就能看出来效果

LayoutColumnStretch在ui_MainWindow.h中自动生成的代码是:      

gridLayout->setColumnStretch(1, 1);

 

表示设置第0列、第1列两者在水平方向的空间比例分配。

 

 

LayoutRowMinimumHeight在ui_MainWindow.h中自动生成的代码是:

 

        gridLayout->setRowMinimumHeight(0, 1);

        gridLayout->setRowMinimumHeight(1, 2);

        gridLayout->setRowMinimumHeight(2, 3);

表示在第0行、第1行、第2行的最小高度是1pixels,2pixels,3pixels

LayoutColumnMinimumWidth在ui_MainWindow.h中自动生成的代码是:

 

  gridLayout->setColumnMinimumWidth(0, 4);

        gridLayout->setColumnMinimumWidth(1, 5);

表示设置第0列、第1列的最小宽度是4pixels、5pixels

 

 

LayoutSizeConstraint在ui_MainWindow.h中自动生成的代码是:

gridLayout->setSizeConstraint(QLayout::SetDefaultConstraint);

 

This property holds the resize mode of the layout.看下表

 

 

 

 

enum QLayout::SizeConstraint

The possible values are:

Constant Value Description
QLayout::SetDefaultConstraint0The main widget's minimum size is set to minimumSize(), unless the widget already has a minimum size.
QLayout::SetFixedSize3The main widget's size is set to sizeHint(); it cannot be resized at all.
QLayout::SetMinimumSize2The main widget's minimum size is set to minimumSize(); it cannot be smaller.
QLayout::SetMaximumSize4The main widget's maximum size is set to maximumSize(); it cannot be larger.
QLayout::SetMinAndMaxSize5The main widget's minimum size is set to minimumSize() and its maximum size is set tomaximumSize().
QLayout::SetNoConstraint1

The widget is not constrained.

 

 

 

QFormLayout属性介绍

1、QFormLayout类管理输入型控件和它的label组成的那些form表格,包括它的界面参数如下图

 

 

2、界面中对应的代码如下表,

 

 

Cpp代码   收藏代码
  1. formLayout = new QFormLayout(widget1);  
  2.         formLayout->setSpacing(6);  
  3.         formLayout->setContentsMargins(11, 11, 11, 11);  
  4.         formLayout->setObjectName(QString::fromUtf8("formLayout"));  
  5.         formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);  
  6.         formLayout->setRowWrapPolicy(QFormLayout::DontWrapRows);  
  7.         formLayout->setContentsMargins(0, 0, 0, 0);  
  8.         label_4 = new QLabel(widget1);  
  9.         label_4->setObjectName(QString::fromUtf8("label_4"));  
  10.   
  11.         formLayout->setWidget(0, QFormLayout::LabelRole, label_4);  
  12.   
  13.         lineEdit = new QLineEdit(widget1);  
  14.         lineEdit->setObjectName(QString::fromUtf8("lineEdit"));  
  15.   
  16.         formLayout->setWidget(0, QFormLayout::FieldRole, lineEdit);  
  17.   
  18.         label_5 = new QLabel(widget1);  
  19.         label_5->setObjectName(QString::fromUtf8("label_5"));  
  20.   
  21.         formLayout->setWidget(1, QFormLayout::LabelRole, label_5);  
  22.   
  23.         comboBox = new QComboBox(widget1);  
  24.         comboBox->setObjectName(QString::fromUtf8("comboBox"));  
  25.   
  26.         formLayout->setWidget(1, QFormLayout::FieldRole, comboBox);  

 

3、其中值得一说的是:LayoutFieldGrowthPolicy属性

 

enum QFormLayout::FieldGrowthPolicy

This enum specifies the different policies that can be used to control the way in which the form's fields grow.

Constant Value Description
QFormLayout::FieldsStayAtSizeHint0The fields never grow beyond their effective size hint. This is the default forQMacStyle.
QFormLayout::ExpandingFieldsGrow1Fields with an horizontal size policy of Expanding or MinimumExpanding will grow to fill the available space. The other fields will not grow beyond their effective size hint. This is the default policy for Plastique.
QFormLayout::AllNonFixedFieldsGrow2All fields with a size policy that allows them to grow will grow to fill the available space. This is the default policy for most styles.

 

 

 

4、还有一个属性值得说:LayoutRowWrapPolicy

 

This property holds the way in which the form's rows wrap.

//这个属性设置了表格如何排版各个元素

If you want to display each label above its associated field (instead of next to it), set this property to WrapAllRows.

//如果你想把每个标签放在相关字段的上方,而不是和它相邻,就设置这个属性值为WrapAllRows。

 

enum QFormLayout::RowWrapPolicy

This enum specifies the different policies that can be used to control the way in which the form's rows wrap.

Constant Value Description
QFormLayout::DontWrapRows0Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles and QS60Style.
QFormLayout::WrapLongRows1Labels are given enough horizontal space to fit the widest label, and the rest of the space is given to the fields. If the minimum size of a field pair is wider than the available space, the field is wrapped to the next line. This is the default policy for Qt Extended styles and andQS60Style.
QFormLayout::WrapAllRows2Fields are always laid out below their label.
  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值