2 Attachment(s)
Problems with QGridLayout
Hi community,
I am having some issue placing 5 items in a grid layout.
Each item is an horizontal layout composed by a QComboBox and a QLabel.
I would like to place the items in this way:
| H_1 | <==> | H_2 |
| H_3 | <==> | H_4 |
| H_5 | <=======|
The arrows between H1,H2 and H3,H4 indicate that the items should be pushed to the extremes when the grid is resized while H5 should be pushed to the left side.
I also set the SizeAdjustPolicy of the 5 combo boxes to the AdjustToContents flag to adjust its size fit to fit the content, but seems not having effect in my code.
The grid layout will be then added to a vertical layout.
Below the code snippet I wrote:
Code:
// The 5 horizontal layouts
// The 5 combo boxes
cb_time_bias
->setSizeAdjustPolicy
(QComboBox::AdjustToContents);
cb_range_bias
->setSizeAdjustPolicy
(QComboBox::AdjustToContents);
cb_rangeGain_bias
->setSizeAdjustPolicy
(QComboBox::AdjustToContents);
cb_azimuth_bias
->setSizeAdjustPolicy
(QComboBox::AdjustToContents);
cb_eccentricity_bias
->setSizeAdjustPolicy
(QComboBox::AdjustToContents);
// The 5 labels
lbl_time_bias
= new QLabel("Set time for all radars");
// label1lbl_range_bias
= new QLabel("Set range for all radars");
// label2lbl_rangeGain_bias
= new QLabel("Set range gain for all radars");
// label3lbl_azimuth_bias
= new QLabel("Set azimuth for all radars");
// label4lbl_eccentricity_bias
= new QLabel("Set eccentricity for all radars");
// label5
timeBiasHBox->addWidget(cb_time_bias);
timeBiasHBox->addWidget(lbl_time_bias);
rangeBiasHBox->addWidget(cb_range_bias);
rangeBiasHBox->addWidget(lbl_range_bias);
rangeGainBiasHBox->addWidget(cb_rangeGain_bias);
rangeGainBiasHBox->addWidget(lbl_rangeGain_bias);
azimuthBiasHBox->addWidget(cb_azimuth_bias);
azimuthBiasHBox->addWidget(lbl_azimuth_bias);
eccentricityBiasHBox->addWidget(cb_eccentricity_bias);
eccentricityBiasHBox->addWidget(lbl_eccentricity_bias);
// Layout the items into the grid
grid->setHorizontalSpacing(50);
grid->addLayout(timeBiasHBox, 0, 0);
grid->addLayout(rangeBiasHBox, 0, 1);
grid->addLayout(rangeGainBiasHBox, 1, 0);
grid->addLayout(azimuthBiasHBox, 1, 1);
grid->addLayout(eccentricityBiasHBox, 2, 0, 2, 1, Qt::AlignLeft);
// Add the grid to the vertical layout
radarBiasVerticalLayout->addLayout(grid);
The final result of my code is the one in this pictureAttachment 13729 while it should be like in this other pictureAttachment 13730
I can not set the comboboxes to the correct size to have the correct result.
I hope to get some help here.
Thanx in advance,
Franco
Re: Problems with QGridLayout
Hi, I think you can achieve the layout you want by playing with the horizontal stretch factors of the widgets, especially the labels.
Ginsengelf
Re: Problems with QGridLayout
I think the problem is that each of the hbox layouts scales its contents independently of all of the others, so when you put them into the grid, nothing lines up.
I would get rid of all of the horizontal layouts and simply put each item separately into a grid cell. This will guarantee that each column of the grid resizes to the size of the largest item in each column and that all of the columns will align vertically. You can play around with the stretch factors or maximum widths for each column to get them to stretch the way you want.