Qt布局

Qt的基本布局

  • QLayout
    • QBoxLayout
      • QHBoxLayout
      • QVBoxLayout
    • QGridLayout

布局中常用的方法

  1. addWidget 向布局中添加需要布局的控件
  2. addLayout 向布局中添加需要布局的子布局
void addWidget(
 QWidget *widget,//需要插入的控件对象
 int fromRow, //插入的行列
 int fromColumn,
 int rowSpan, //占用的行数,列数
 int columnSpan, 
 Qt::Alignment alignment = Qt::Alignment()//描述控件的对齐方式
 )
This is an overloaded function.
This version adds the given widget to the cell grid, spanning multiple rows/columns. 
The cell will start at fromRow, fromColumn spanning rowSpan rows and columnSpan columns.
The widget will have the given alignment.
If rowSpan and/or columnSpan is -1, then the widget will extend to the bottom and/or right edge, respectively.
void addLayout(
QLayout *layout,
 int row, 
 int column, 
 int rowSpan,
 int columnSpan,
 Qt::Alignment alignment = Qt::Alignment())
This is an overloaded function.
This version adds the layout layout to the cell grid, spanning multiple rows/columns.
 The cell will start at row, column spanning rowSpan rows and columnSpan columns.
If rowSpan and/or columnSpan is -1, then the layout will extend to the bottom and/or right edge, respectively.
     //hostLabel放在第一行第一列
    Layout->addWidget(hostLabel,0,0); 

    //LineEditLocalHostName放在第一行第二列
    Layout->addWidget(LineEditLocalHostName,0,1);

    //Btndetail放在第三行第一列,占据一行两列
    Layout->addWidget(Btndetail,2,0,1,2);

设置控件缩放因子

每个布局都有相应的拉伸因子设置函数。下面是QGridLayout里面的设置函数:

void setColumnStretch ( int column, int stretch );

void setRowStretch ( int row, int stretch );

具体使用如下

Layout->setColumnStretch(0,1);

Layout->setColumnStretch(1,3);

这样在窗体反正改变的时候,hostLabel与LineEditLocalHostName会按1:3的比例发生改变。

并且,在不进行其他尺寸设置的时候(最大值最小值固定什么的),控件也会以这个比例显示在界面上。

NetworkInfo

附github:QT获取本机网络信息的demo

猜你喜欢

转载自blog.csdn.net/erice_s/article/details/80219549