Qt 使用布局管理器失效或者异常

1.使用布局管理器失效


继承与mainwindow或者再mainwindow的窗口,使用布局管理器将不起作用。
mainwindow窗口有自己的布局管理方式,mainwindow中有个中心部件。
如果要在mainwindow中使用布局,可以new 一个widget部件,然后在该部件上使用布局管理器;最后设置该部件为中心部件。

2.使用布局管理器后,不能设置其中部件大小(setGeometry失效)。


使用布局管理器后,部件位置大小将由布局管理器控制。如果要指定部件的大小,可以使用setFixedsize函数。
 

void QWidget::setFixedSize(const QSize &s)
Sets both the minimum and maximum sizes of the widget to s, thereby preventing it from ever growing or shrinking.
This will override the default size constraints set by QLayout

/*
设置部件的最大,最小尺寸都为s,用来防止拉伸及扩大。
这个操作将会覆盖布局管理器默认的限制。
*/

3.设置布局管理位置大小属性无效(layout setGeometry)

布局管理器的位置属性继承于父部件的大小(针对:QBoxLayout/QGridLayout)。

QBoxLayout/QGridLayout 官网介绍:
takes the space it gets (from its parent layout or from the parentWidget())
/* 部件的大小来自于父部件,或者父layout */

既然布局管理器大小由父部件空间大小决定,那么我们可以先在new一个部件,然后设置该部件的位置大小,最后在该部件上添加布局管理器。

4.布局管理器上添加各种部件后,例如水平布局管理器,部件大小位置异常。

布局管理器含有“margin”的边缘。

qt官网介绍:

void QLayout::setContentsMargins(int left, int top, int right, int bottom)
Sets the left, top, right, and bottom margins to use around the layout.
By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.
This function was introduced in  Qt 4.3

/*
设置上下左右边距,qt默认是11px
*/

可以使用以下函数,修改边距。

setMargin()函数(官方建议不要使用,存在是为了兼容旧版本)
setContensMargin()

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值