I am using QFormLayout
with QLabels
in the left column and various widgets in the right column. On the right, there are either labels, check boxes, combos or line edits. Unfortunately each of there controls has different natural height. But I would like to have each row in the form layout to have equal heights determined by the biggest one (I know in which row it is). Is there any simple way to achieve this? I cannot find anything like QFormLayout::setRowHeight()
.
Asked
Active
Viewed 1,211 times
0

HiFile.app - best file manager
- 7,273
- 2
- 32
- 80
-
Could you use `QGridLayout` instead? This answer is somewhat (it's about width, not height) related https://stackoverflow.com/questions/34644808/set-vertical-alignment-of-qformlayout-qlabel . – Ronny Brendel Jan 30 '18 at 19:25
-
@V.K., try with `layoutVerticalSpacing` or `layoutFieldGrowthPolicy`, sorry I never used them, but big chance they might help! – Mohammad Kanan Jan 30 '18 at 23:07
1 Answers
1
One solution, just assign equal size to all widgets at runtime using the following function:
void setEqualRowHeight(QFormLayout *formLayout, int height)
{
QWidget *w;
for(int i = 0; i < formLayout->rowCount(); i++) {
QLayoutItem *item = formLayout->itemAt(i, QFormLayout::FieldRole);
if (item && (w = item->widget())) {
w->setFixedHeight(height);
}
}
}

putu
- 6,218
- 1
- 21
- 30
-
1This will probably expand the input-widgets and break the "natural height" they have on the system. I'd rather set the fixedHeight on the labels only of the form layout. – ymoreau Jan 31 '18 at 08:56
-
@ymoreau Yes, it will break the natural height. Since OP wants to have equal height for all rows, if we set fixed height for the label, it won't fulfill the requirement. – putu Jan 31 '18 at 09:04
-
1From what I understood OP wants the layout-rows to have the same height not the widgets to have the same height but maybe I got it wrong. – ymoreau Jan 31 '18 at 09:15
-
I tried setting fixed height for the field widgets (not labels) and it works fine so far. For example checkboxes (field role) are not resized. On the other hand, whan I tried setting fixed height on the labels, the checkboxes are aligned to the top of the row instead of the middle... Well, the accepted solution probably in not a silver bullet suitable for all but for my case it works. – HiFile.app - best file manager May 04 '18 at 08:05