C++ 使Qlayout不随父对象调整大小

C++ 使Qlayout不随父对象调整大小,c++,animation,qt5,c++17,qlayout,C++,Animation,Qt5,C++17,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(

考虑以下代码-

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;
};
class-collablegroupbox:public-QWidget
{
公众:
可折叠组框(QWidget*parent=nullptr):QWidget(parent)
{
auto chk=新的QCheckBox(本);
chk->move(0,0);
chk->调整大小(宽度(),12);
setContentsMargins(0,chk->height(),0,0);
QPropertyAnimation*a=新的QPropertyAnimation(该“尺寸”);
a->设置持续时间(1000);
连接(chk,&QCheckBox::切换,[this,chk,a](bool c){
静态int s=高度();
如果(自动l=layout()){
如果(c){
s=高度();
a->setEndValue(QSize(width(),chk->height());
a->start();
//设置最大高度(chk->height()*2);
}否则{
qDebug()setEndValue(QSize(width(),s));
a->start();
//设置最大高度;
}
}
});
}
bool isChecked()常量;
无效设置已检查(布尔已检查);
私人:
布尔穆查过;
};

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

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

注:如果有人有更好的解决方案,请张贴