How to make column widths of Qt grid width

I have QDialog

with a grid layout. The grid width is 5 columns for a variable number of rows. QDialog

starts with a pre-built user interface that has a label in each of 5 columns. The rest of the grid is constructed as follows. For each added line:

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

When it is a full column, 0 is the widest column. Columns 1-4 are the same width. Some combo boxes (cols 1-3) are too wide to be seen and displayed as "...". The column of the checkbox (4) is wider than it should be. I've tried using QWidget::adjustSize()

for every damn widget, including the dialog itself, and I can't get it to auto-size to fit all. While trying to debug, I printed out the width of the widget for each column, but the values ​​I see are not what I see on the screen. I was under the impression that the grid layout automatically adjusts every widget in every column to be wide enough to fit the widest element, but that doesn't seem to be the case. Is there some property setAutoColumnWidth

or something else?

+3


source to share


1 answer


If you want to set the width for the columns of the layout, you can set the Stretch values ​​for the widgets inside the layout. For example, set the horizontal braces of all widgets to 1

for equal width:

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.

+1


source







All Articles