Reserve space for initially hidden widget in QVBoxLayout

Posted by Skinniest Man on Stack Overflow See other posts from Stack Overflow or by Skinniest Man
Published on 2010-04-26T22:50:34Z Indexed on 2010/04/26 22:53 UTC
Read the original article Hit count: 205

Filed under:
|
|

I am using a QVBoxLayout to arrange a vertical stack of widgets. I want some of them to be initially hidden and only show up when a check box is checked. Here is an example of the code I'm using.

MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { QVBoxLayout *layout = new QVBoxLayout(this);

QLabel *labelLogTypes = new QLabel(tr("Log Types"));

m_checkBoxCsv = new QCheckBox(tr("&Delimited File (CSV)")); m_labelDelimiter = new QLabel(tr("Delimiter:")); m_lineEditDelimiter = new QLineEdit(","); checkBoxCsv_Toggled(m_checkBoxCsv->isChecked()); connect(m_checkBoxCsv, SIGNAL(toggled(bool)), SLOT(checkBoxCsv_Toggled(bool)));

QHBoxLayout *layoutDelimitedChar = new QHBoxLayout(); layoutDelimitedChar->addWidget(m_labelDelimiter); layoutDelimitedChar->addWidget(m_lineEditDelimiter);

m_checkBoxXml = new QCheckBox(tr("&XML File")); m_checkBoxText = new QCheckBox(tr("Plain &Text File"));

// Now that everything is constructed, put it all together // in the main layout. layout->addWidget(labelLogTypes);

layout->addWidget(m_checkBoxCsv); layout->addLayout(layoutDelimitedChar);

layout->addWidget(m_checkBoxXml); layout->addWidget(m_checkBoxText);

layout->addStretch(); }

MyWidget::checkBoxCsv_Toggled(bool checked) { m_labelDelimiter->setVisible(checked); m_lineEditDelimiter->setVisible(checked); }

I want m_labelDelimiter and m_lineEditDelimiter both to be initially invisible and I want their visibility to toggle with the state of m_checkBoxCsv.

This code acheives the functionality I desire, but it doesn't seem to reserve space for the two initially hidden widgets. When I check the checkbox, they become visible, but everything is kind of scrunched to accomodate them.

If I leave them initially visible, everything is laid out just the way I would like it. Is there any way to make the QVBoxLayout reserve space for these widgets even if they're initially invisible?

© Stack Overflow or respective owner

Related posts about qt

Related posts about layout