I am using a QVBoxLayout to arrange a vertical stack of widgets. The QVBoxLayout is contained within a QScrollArea. I want some of the widgets 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. When they become visible, I would like the layout to expand vertically to accommodate them.
This code achieves 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 accommodate 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?
If I don't put this widget into a QScrollArea, then this code works exactly as I want it to. What is the deal with the QScrollArea?