Qt 使用布局管理器,控件大小未能自适应变化

问题:

        使用布局管理器管理子部件,使用了QVHlayout管理器,发现控件大小固定,即使通过拖动窗口也不能改变控件的大小,布局管理器不是自动控制部件的大小吗?为什么没有生效?

如图所示:


解决办法:

对子部件添加延申策略
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

分析过程:

1.控件外观由 margin ,border,padding,content构成,可能是margin被拉伸了。但官方文档介绍默认 margin ,border,padding为0;即三者重合。
通过设置border-with,colo(上图红色部分)也验证了该猜想。

2.布局管理器的锅,为什么设置了伸展因子也不能拉升控件的大小?

官方文档对addWidget介绍:
void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())
        Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment.
        The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.
If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

        The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.

 解释说:

        如果stretch设置为0且在布局管理器中没有比0大的伸展因子,那么布局管理器就会使用子部件提供的sizePolicy()划分空间。

官方文档对QSizePolicy::Policy解释:

      

 expanding解释:

sizeHint()是一个合理的大小,但小部件可以缩小,仍然有用。小部件可以利用额外的空间,因此它应该获得尽可能多的空间(例如,水平滑块的水平方向)。

那么试试它:

效果:

 

奇怪的事:
官方文档:

void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

解释这个函数说:
        If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

        如果 stretch伸展因子设置为0,且在QBoxlayout中没有比0大的伸展因子。那么布局管理器将根据子部件设置的sizePolicy()策略划分空间。
但是我用layout->addWidget(drag_wight,0,Qt::AlignCenter),却不能自适应变化。

效果:

 代码:

 DragWidget * drag_wight = new DragWidget(this);
    DragWidget * drag_wight1 = new DragWidget(this);
    DragWidget * drag_wight2 = new DragWidget(this);
    DragWidget * drag_wight3 = new DragWidget(this);
    drag_wight->setStyleSheet("border-style: outset;border-width: 2px;border-color: red;");
    drag_wight1->setStyleSheet("border-style: outset;border-width: 2px;border-color: red;");
    drag_wight2->setStyleSheet("border-style: outset;border-width: 2px;border-color: red;");
    drag_wight3->setStyleSheet("border-style: outset;border-width: 2px;border-color: red;");

    drag_wight->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    drag_wight1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    drag_wight2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    drag_wight3->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    QBoxLayout *layout = new QVBoxLayout;

    //layout->setStretch()
    //drag_wight3->
    layout->addWidget(drag_wight,Qt::AlignCenter);
    layout->addWidget(drag_wight1,Qt::AlignCenter);
    layout->addWidget(drag_wight2,Qt::AlignCenter);
    layout->addWidget(drag_wight3,Qt::AlignCenter);
    layout->setMargin(5);
    layout->setSpacing(5);
    setLayout(layout);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值