Spare space for an originally hidden widget in QVBoxLayout in QScrollArea

I use QVBoxLayout to arrange the vertical stack of widgets. QVBoxLayout is contained in QScrollArea. I want some of the widgets to be hidden initially and only show when the checkbox is selected. 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 to m_labelDelimiter, and m_lineEditDelimiterboth were initially invisible, and I want them to be switched to the state of visibility m_checkBoxCsv. When they become visible, I would like the layout to expand vertically to fit them.

This code achieves the desired functionality, but does not seem to leave room for the two initially hidden widgets. When I check the box, they become visible, but they all look smoothed to fit them.

, , . QVBoxLayout , ?

QScrollArea, , . QScrollArea?

+3
3

, . , , , , QScrollArea. scrollArea->setWidgetResizable(true), .

, .

+4

.

1) , . , . / , .

2) , , . , , . , ( ).

3) , , , , . . , . , , /, , .

- . , , , .

+5

spacer layoutDelimitedChar , smth :

QHBoxLayout *layoutDelimitedChar = new QHBoxLayout();

layoutDelimitedChar->addSpacerItem(new QSpacerItem(0, 33, QSizePolicy::Maximum, QSizePolicy::Maximum));

layoutDelimitedChar->addWidget(m_labelDelimiter);
layoutDelimitedChar->addWidget(m_lineEditDelimiter);

, ,

+1
source

Source: https://habr.com/ru/post/1742893/


All Articles