Centering in a layout
-
Hi,
Aren't you looking for QFormLayout ?
-
@SGaist said in Centering in a layout:
Aren't you looking for QFormLayout ?
No, this really isn't a form. The two widgets have nothing to do with each other. I might just as well come back and ask for, say, a third widget on the left as well, and then the principle of two-column
QForm
wouldn't apply. -
@mrjj
I know. If you think out the math/logic, this approach works best as the left-hand ratio tends to infinity (right-hand one stays at 1), because we're really try to center the first item across the whole width. I think/worry about these things... ;-) -
I would recommend using
QGridLayout
because you actually can add several widgets to the same grid.
But this cannot be done in the normal way, you will need to edit the ui file as plain text.- Add QLabel and QCheckBox to the QGridLayout
- Save & Close Designer
- Open the ui file with Plain Text Editor
- Find the grid layout and make the QLabel and QCheckBox both at the same grid, for example, row 0 and column 0.
- Save, and reopen the ui file with Designer
- Set the alignment of QLabel to Center Horizontally and QCheckBox to Right
-
@Bonnie
I will certainly try any suggestion from you!Find the grid layout and make the QLabel and QCheckBox both at the same grid, for example, row 0 and column 0.
So you want me to put both widgets in a single cell of a (presumably) 1x1
QGridLayout
, right? And then set the alignments on each widget, and hey presto that will do it? The label will get centered over the whole width of the cell, and the checkbox will get put at the right?Oh.... hang on... you want them kept in separate cells, but both cells to have the same row #0, column #0? So I have two separate cells at the same place? Surely that's cheating, or is it "super-imposition"...???
-
@JonB
I've cheated a lot by QGridLayout :)
So finally the ui content will be... <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0" alignment="Qt::AlignRight"> <widget class="QCheckBox" name="checkBox"> <property name="text"> <string>CheckBox</string> </property> </widget> </item> <item row="0" column="0" alignment="Qt::AlignHCenter"> <widget class="QLabel" name="label"> <property name="text"> <string>TextLabel</string> </property> </widget> </item> </layout> ...
-
@Bonnie
Yeah got it.... I don't have Qt available now, could you just tell me, what code is it producing in theui
file for that? What is the runtime call to get multiple widgets into aQGridLayout
row,col 0,0 cell? Is it some "appendWidget()` or what?Designer has a way of renumbering rows & columns and I wouldn't want to rely on those matching numbers remaining over time. I'll look at coding in initialization to make sure they're in the right place, once I know how we're actually doing it in code.
-
-
I have had a play this morning....
@Bonnie
I tried out your suggestion. It seems to work, perfectly! You get the bonus points for providing what seems like the correct solution. However, I cannot rely on not altering the UI in Designer, intentionally or unintentionally, over time, and finding it messes up the two-widgets-in-one-cell :(@mrjj
I have gone for a solution related to yours. But changed to be a 3-columnQGridLayout
(didn't seem to work right with aQHboxLayout
), withlayoutColumnStretch 2,6,2
, so 20%-60%-20%, like:<layout class="QGridLayout" name="gridLayout_2" columnstretch="2,6,2"> <item row="0" column="0"> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> </spacer> </item> <item row="0" column="1" alignment="Qt::AlignHCenter"> <widget class="QLabel" name="label"> </widget> </item> <item row="0" column="2" alignment="Qt::AlignRight"> <widget class="QCheckBox" name="cbLockMode"> </widget> </item> </layout>
It may be this just works given the widths of my particular label & checkbox, but I have tried stretching the container and things seem to resize maintaining the label at approximately in the center of the whole.
Thank you both, this will have to do as a reasonable compromise.