I want to make a widget that contains 0 to N child QTextEdit so that QTextEdits don't occupy all available space. Main widget should manage both scrollbars. In other words, I want vertical layout to only manage children placement but not their size, their size should be managed by its contents (text in that case). Contents can be dynamic and changed in runtime
My code looks like that:
scroll_area = new QScrollArea();
scroll_widget = new QWidget();
scroll_vbox = new QVBoxLayout(scroll_widget);
scroll_vbox->setContentsMargins(0, 0, 0, 0);
scroll_vbox->setSpacing(0);
QTextEdit *chunk0 = new QTextEdit();
chunk0->setText("line 1\nline 2");
chunk0->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum);
chunk0->setWordWrapMode(QTextOption::NoWrap);
scroll_vbox->addWidget(chunk0);
QTextEdit *chunk1 = new QTextEdit();
chunk1->setText("line 1\nline 2\nline 3\nline 4");
chunk1->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum);
chunk1->setWordWrapMode(QTextOption::NoWrap);
scroll_vbox->addWidget(chunk1);
QTextEdit *chunk2 = new QTextEdit();
chunk2->setText("looooooooooooooooong text");
chunk2->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum);
chunk2->setWordWrapMode(QTextOption::NoWrap);
scroll_vbox->addWidget(chunk2);
QTextEdit *chunk3 = new QTextEdit();
chunk3->setText("even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger teeeeeeeeeeeeeeeeeeeeeeeeexxxxxxxxxxxxxxxtttttttttttt");
chunk3->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum);
chunk3->setWordWrapMode(QTextOption::NoWrap);
scroll_vbox->addWidget(chunk3);
QTextEdit *chunk4 = new QTextEdit();
chunk4->setText("line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\nline 10\nline 11\nline 12\nline 13\nline 14\nline 15");
chunk4->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum);
chunk4->setWordWrapMode(QTextOption::NoWrap);
scroll_vbox->addWidget(chunk4);
scroll_area->setWidget(scroll_widget);
scroll_area->setWidgetResizable(true);
vbox0 = new QVBoxLayout(main_widget);
vbox0->addWidget(scroll_area);
Below are images of what I want and what I get
Wanted layout:

Actual layout:

There are a few problems with my layout:
- Children are not resized to have minimum required size. If I add only one item, it gets expanded to the full parent width ignoring it's contents (text)
- Children have scrollbars. I can hide them but they will still work with mouse wheel and parent widget wouldn't create a global scrollbar for them
If I made no mistake, what you want should look like the code below. In total, I have:
int main(int argc, char** argv)function for ease of testing. Your inital code has been placed in extra brackets{}.Note that this forced me to declare variables separately from their initialization.
chunks the same way with less code.setHorizontalScrollBarPolicyandsetVerticalScrollBarPolicy.Here is the code:
What I did not do, however, is implement an optimization to detect when the widget size does not need to be changed, i.e. when text is being edited without changing the number of line (height does not change) and not on the longest line of text (width does not change).