Dinendal Dinendal - 2 months ago 35
C++ Question

Qt - Resize layout when adding items

I have a vertical layout I'm filling with file names.
I have a QFileDialog to select files, then the slot is adding names to the layout.

void convertInterface::on_browseButton_clicked()
fileNames = QFileDialog::getOpenFileNames(this, tr("Open File"), "./FilesToConvert", tr("Test Files (*.txt)"));
for each (QString item in fileNames)
QLabel *name = new QLabel(this);

The result with 3 files :

enter image description here

The result with 3 more files, so 6 total :

enter image description here

Overall layout :

enter image description here

How to resize the layout automatically ?


You need some kind of automatic layout on the parent widget. For example, if mainLayout is a child widget of centralLayout, set the centralLayout to have vertical layout (or any other automatic layout). Than it will expand automatically.

If you break the layout of the parent, the size of the child widgets is fixed and does not update automatically.