【问题标题】:make Qlayout not resize with parent使 Qlayout 不与父级一起调整大小
【发布时间】:2020-06-26 06:51:52
【问题描述】:

考虑以下代码 -

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;
};

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

【问题讨论】:

    标签: c++ animation qt5 c++17 qlayout


    【解决方案1】:

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

    PS:如果有人有更好的解决方案,请发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多