Widgets alignment within layout
-
Hello. I have issues with my widgets not being properly aligned and I cannot fully figure out how it works..
Take a look at my widget.ui
As you can see, I have placed some widgets within vertical layout and I placed horizontal spacers hoping that they will be automatically aligned but that is not the case. When I run my application:
With default font:
If I try to change font with:
//QFont font("Courier New"); //QApplication::setFont(font);
It is even worse:
My questions:
- How can I ensure that comboboxes are aligned nicely?
- How can I manually modify the width of horizontal spacers? For example, I would like to move the spacer to the position that I have marked:
I cant do that by simply moving the spacer as it permamently "stuck" to the position
-
@lukutis222 I think for your use case https://doc.qt.io/qt-6/qformlayout.html is a better choice
-
@lukutis222 said:
- How can I ensure that comboboxes are aligned nicely?
Use a layout that has columns. Like @jsulm said QFormLayout is best suited for two column label+widget layouts. In cases like yours, where you have more columns and you want a spacer at the end, a QGridLayout would do nicely.
- How can I manually modify the width of horizontal spacers?
You don't, or rather shouldn't. Layouts resize elements according to their policies. The default policy for spacers is expanding, meaning the widgets in the layout take the minimum space they can get by with, and the rest is filled by the spacer. Think of it as a spring that expands as much as it can. If you want smaller spacer increase the minimum sizes of the widgets.
You can set a fixed size of an element by setting min and max to the same values, but that would defeat the purpose of a layout or a spacer.
-
@jsulm @Chris-Kawa
Thanks both. I have easily managed to sort my widget by using grid layout as you have suggested