Synchronize QFormLayouts (i.e. width of labels and widgets)
I am starting to use Qt Designer (with Qt 5.2.1) for my GUIs (sadly Qt Quick didn't meet my requirements ;( ) and I stumbled upon a problem I did encounter before. Here a little screenshot:
!http://abload.de/img/formlayoutsllk2l.png(QFormLayouts not synchronized)!
I am using two form layouts on two separate group boxes. And, aside from very ugly "hacks", I have not yet found a way to make them use the same widths for their widgets. And after all I know I think there is no easy/pretty way to achieve this. I really hope somebody proves me wrong - I don't want to use a different type of layout. :)
PS: It would be nice to be able to "link" different form layouts (a bit like you do with buddies). Ideas on how to do this within a subclass of QFormLayout would be welcome, too.
PPS: "Hacks" that are only slightly ugly would be helpful, too.
Do you mean something like the Qtopia style or windows style ?
The techniques are explained in the detailed documentation for QFormLayout (field growth policy, form alignment, label alignment)
Hope it helps
I never heard of Qtopia, but what I mean is: I want all the labels and widgets of several form layouts to align perfectly. It just looks amateurish to me otherwise (like the screenshot above shows). So in the above image the widgets in the lower layout are wider than the ones in the upper layout.
It should look like this (I am not talking about the spacers but the width of the widgets on the right)!
!http://abload.de/img/goodlayoutsr4zv7.png(Prettified form layouts)!
But that just seems impossible (at least I have not yet found a satisfying solution).
I see what you mean. However, did you look at the QFormLayout documentation ? You'll find the properties you can modify on this class to get what you want.
Sure I took a look at it. But I really don't see how the properties could help. Could you be a bit more specific?
FieldGrowthPolicy -> how your widget should behave -> ExpandingFieldsGrow (you may have to modify your widget size policy)
FormAlignment -> how your form should be aligned -> horizontally centered to the left
LabelAlignment -> how the labels should be aligned -> to the left
But I don't think these are related to the problem. They only influence alignment and how far the widgets (not the labels) reach to the right. That's not really of concern.
Just to be clear: I marked the problem. The form layout chooses the necessary space according to all the labels inside itself, and that's true for all the layouts. But they don't look for sizes inside other layouts, that why the widgets don't align. There is no "anchor" or "stretch factor" I know of to circumvent that. It would be perfect if several widgets could share a single layout (so it knows about all child widgets it has to lay out), or if form layouts could have buddies. Or did I miss some kind of stretch factor that lets me influence the space distribution?
Ok, I see what you mean. Currently (unless you want to create your own layout class which would be the cleanest way) the only not so dirty hack I can think of would be to get the widest string from both form layouts and then pad one of the other layout with spaces to match the longest.
Hope it helps
That's "not so dirty"? Now I am interested to see what you consider dirty.^^
In addition my application can be translated, so static solutions are impractical.
Writing a custom layout might be the only real solution. If the different layouts cached their maximum label-width in a way that would be accessible from "friend" layouts, this might even be doable. But I suspect that these details are hidden in private classes and this seemingly easy thing will get really ugly and/or difficult.
I also wouldn't know how to use a custom layout in Qt Designer, I am fairly new to it.
Is there something like a static variable that can only be accessed by some instances of a class instead of all?
No you're not ;)
You can also use a QGridLayout, more code but you have more control over the row/col setup
Yes, I know. But I have had this problem with QFormLayout before and I thought I am just missing something - it looks like the form layout is mostly useless to me, no matter how much I like its simplicity. Can one request features somewhere?^^
Also, I am not too sure if a grid layout wouldn't suffer from the same problem... if I had to hardcode the widths of columsn, it wouldn't be much better than the form layout I am using currently.
PS: "I am not?" - what was that refering to?
Feature request can be done on the "bug report system":http://bugreports.qt-project.org :)
With the grid layout, you can play with span and stretch factor
You're not interested by what I consider dirty hacks ;)
[quote author="thEClaw" date="1400572586"]Also, I am not too sure if a grid layout wouldn't suffer from the same problem[/quote]
No it not suffer :-) See "QGridLayout::setColumnMinimumWidth":http://qt-project.org/doc/qt-4.8/qgridlayout.html#setColumnMinimumWidth
Also for form layout you could set the minimum width of all labels
foreach (QLabel* label, yourDialog->findChildren<QLabel*>())
I seemingly need an additional account for the bug report system. Maybe...
Setting a minimum width still is not as flexible as a form layout. As I said, translations are an option, and those might exceed the minimum width of one or several layouts (so the result would not be aligned again).
I might go the "custom layout route". Or just be angry with Qt for a day or two and then accept my fate.
[quote author="thEClaw" date="1400574466"]translations are an option, and those might exceed the minimum width of one or several layouts (so the result would not be aligned again).[/quote]
Ok, so you know the maximum width of every label text:
int width = fontMetrics.width(label->text());
But then the layouts have to "know" each other to find a common value or the problem I initially wanted to solve continues to exist.
These "hacks" are less ugly, but a custom layout would probably be the only clean solution.
I don't get your thoughts. From my point of view, this "hack" will work seemly.
Other alternative in your Ui design is to remove the group-box and layout the entire dialog in a form layout. Instead of having group box, place a single QLabel with a bold text to mimic the group box
edit: instead of QLabel I saw you have QCheckBox. So you can put a check box and enable/disable the controls programatically