背景:
有些数据是不定时加载的,比如连接蓝牙,连接1个蓝牙设备就会添加1个蓝牙的相关信息;
1个蓝牙设备数据与另1个蓝牙设备数据不能间隔太大,但是使用QVBoxLayout就会将上面2个蓝牙占满全屏,即使使用弹簧,也只能满足2个蓝牙向上对其;
假如再次连接多个蓝牙,因为前面有了弹簧,就会使全部蓝牙不能向上对其了.
解决办法:
动态布局.
每次有新的设备连接就重新布局,再最后面添加弹簧,这样就能保证所有数据向上对其
QLayout *layout = m_tFrame->layout();
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->setMargin(0);
vLayout->setSpacing(0);
if (layout) {
//将旧的蓝牙设备数据取出来放在新的QVBoxLayout中
while (QLayoutItem *item = layout->takeAt(0)) {
AdapterWidget *widget = dynamic_cast<AdapterWidget *>(item->widget());
//若当前为nullptr,还需要继续判断后面的数据
if (!widget)
continue;
//当蓝牙属于关闭状态时,设置最小高度
//开启后,设置最大值(破坏setFixedHeight)
if (!widget->getSwitchState()) {
widget->setFixedHeight(minHeight);
} else {
widget->setMaximumHeight(maxHeight);
}