使Qlayout不随父级调整大小



考虑以下代码-

class CollapsibleGroupBox : public QWidget
{
public:
CollapsibleGroupBox(QWidget *parent = nullptr): QWidget(parent)
{
auto chk = new QCheckBox(this);
chk->move(0, 0);
chk->resize(width(), 12);
setContentsMargins(0, chk->height(), 0, 0);
QPropertyAnimation *a = new QPropertyAnimation(this, "size");
a->setDuration(1000); 
connect(chk, &QCheckBox::toggled, [this, chk, a](bool c) {
static int s = height();
if (auto l = layout()) {
if (c) {
s = height();
a->setEndValue(QSize(width(), chk->height()));
a->start();
//setMaximumHeight(chk->height() * 2);
} else {
qDebug() << s;
a->setEndValue(QSize(width(), s));
a->start();
// setMaximumHeight(s);
}
}
});
}
bool isChecked() const;
void setChecked(bool checked);
private:
bool m_checked;
};

现在,当动画开始时,布局也会随着小部件而缩小或增长,我不希望我只需要显示可以适应当前小部件大小的布局部分,而且缩小布局的小部件看起来不太好,我尝试更改大小策略,但似乎找不到任何东西,有人能帮忙吗。

在动画开始时禁用布局(QLayout::setEnabled(,然后在动画结束时重新启用

附言:如果有人有更好的解决方案,请发帖。

最新更新