How to Make Qt Grid Layout Auto Size Column Widths

10,799

If you want to set widths for layout columns, you can set the Stretch values for the widgets inside the layout. For example set the horizontal stretches of all the widgets to 1 for equal widths :

lineEdit->sizePolicy().setHorizontalStretch(1);
comboBox1->sizePolicy().setHorizontalStretch(1);
comboBox2->sizePolicy().setHorizontalStretch(1); 
comboBox3->sizePolicy().setHorizontalStretch(1); 
checkBox->sizePolicy().setHorizontalStretch(1);

If you want a specific column to be wider, set a higher stretch value for the widget in that column.

Share:
10,799
Qman
Author by

Qman

Updated on June 13, 2022

Comments

  • Qman
    Qman about 2 years

    I have a QDialog with a grid layout in it. The grid is 5 columns wide by a variable number of rows. The QDialog begins with a prebuilt UI that has a label in each of the 5 columns. The rest of the grid is built as follows. For each row added:

    • a QLineEdit is added to col 0
    • a QComboBox is added to cols 1-3 and each has a variable and different number of items
    • a QCheckBox is added to col 4

    When this is complete column 0 is the widest column. Columns 1-4 are the same width. Some combo box items (cols 1-3) are too wide to see and show up as "...". The check box column (4) is wider than it needs to be. I've tried using QWidget::adjustSize() on every damn widget in there including the dialog itself and I can't get it to auto size to fit everything. In trying to debug it I printed out the widget's width for each column but the values I'm seeing are no way what I'm seeing on screen. I was under the impression the grid layout would automatically adjust each widget in each column to be just wide enough to fit the widest item but it doesn't seem to be doing that. Is there some sort of setAutoColumnWidth property or something?

  • Qman
    Qman over 9 years
    thanks Nejat, I played around with the relative stretch factors til I got it right. Any idea why the QGridLayout didn't handle this automatically?
  • Nejat
    Nejat over 9 years
    @Qman Layouts are a means of placing the widgets in a form. The sizing is done with size policies, min size and max size of the widgets in a layout. So surely QGridLayout handles it properly. But as you are inserting widgets of different type with different default size policies, they are have not necessarily equal sizes at first place.
  • Qman
    Qman over 9 years
    Thanks once more Nejat. All the size policies were the same, (Preferred, Preferred, 0, 0). The odd thing is that the first two columns (QLineEdit/QComboBox) stretched to display their fields and the last 3 columns (QComboBox/QComboBox/QCheckBox) were the exact same width as one another. That 3rd column was the only one that wasn't wide enough to display it's data. The last column (QCheckBox) was wider than it needed to be. One thing I forgot to mention is that everything is fine on one platform but not on another. I've been trying to figure out the differences but no luck yet.
  • Paulo Carvalho
    Paulo Carvalho over 5 years
    That doesn't work with QChartViews. The first one gets to maximum size, then the ones in the column hearders get the max height and the line headers get the max width. Finally, the others are shrunk to min size.
  • Paulo Carvalho
    Paulo Carvalho over 5 years
    In time: I solved my problem following the same rationale but by using QGridLayout::setRowStretch() and QGridLayout::setColumnStretch().