Resize QWidget during show event?
-
Hi, I have a
QWidget
which has aQScrollArea
, this scroll area contains a vertical layout (QVBoxLayout
) in which I addQCheckBox
during theshowEvent()
. Here is a test code:class ResizeTest : public QWidget { Q_OBJECT public: ResizeTest(QWidget* parent = Q_NULLPTR); ~ResizeTest(); int num{0}; private: Ui::ResizeTest ui; QVector<QPointer<QCheckBox>> chks; protected: void showEvent(QShowEvent* event) override; void closeEvent(QCloseEvent* event) override; private slots: void onCheckBoxClicked(); };
ResizeTest::ResizeTest(QWidget* parent) : QWidget(parent) { ui.setupUi(this); } ResizeTest::~ResizeTest() {} void ResizeTest::showEvent(QShowEvent* event) { if(num > 0) { for(int ii = 0; ii < num; ++ii) { QString name = QString("CheckBox Number ").append(QString::number(ii + 1)); QPointer<QCheckBox> chkPtr = new QCheckBox; chkPtr->setText(name); connect(chkPtr, &QCheckBox::clicked, this, &ResizeTest::onCheckBoxClicked); chks << chkPtr; ui.verticalLayout->addWidget(chkPtr); } } qDebug() << ui.verticalLayout->minimumSize(); // outputs QSize(0, 0) always int widgetHeight = this->layout()->contentsMargins().top() + this->layout()->contentsMargins().bottom() + + ui.verticalLayout->minimumSize().height(); int widgetWidth = this->layout()->contentsMargins().left() + this->layout()->contentsMargins().right() + ui.verticalLayout->minimumSize().width(); this->resize(widgetWidth, widgetHeight); } void ResizeTest::closeEvent(QCloseEvent* event) { while(!chks.isEmpty()) { auto lastItem = chks.last(); if(!lastItem.isNull()) delete lastItem; chks.removeLast(); } chks.clear(); } void ResizeTest::onCheckBoxClicked() { qDebug() << ui.verticalLayout->minimumSize(); // this gives me correct size }
Is there any way I could get the correct
ui.verticalLayout->minimumSize()
during the show event so that my widget is resized properly when shown? -
You should not modify the widgets during the showEvent - do it in an init function or in the ctor instead.
If you use proper layouts and set proper min/max sizes for your widgets then the widget is resized without any interaction from your size. -
@Christian-Ehrlicher Ok. I understand, I will add my check boxes in a different function and not during
showEvent()
.I tried setting
layoutSizeConstraint
to different values for the vertical layout, theQScrollArea
as well as for the widget itself, because ofQScrollArea
my widget is always resized to a small size when shown for the first time, this is why I tried to add and resize the widget duringshowEvent()
myself. According to Qt's documentation:If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you intend to dynamically change the contents of the layout. In such cases, setting the layout's size constraint property to one which provides constraints on the minimum and/or maximum size of the layout (e.g., QLayout::SetMinAndMaxSize) will cause the size of the scroll area to be updated whenever the contents of the layout changes.
But this is not working for me, I am setting the size constraint to QLayout::SetMinAndMaxSize but the
QScrollArea
remains at the minimum size when shown for the first time.