Question

So I am in the works of making my first GUI. So far I have made a groupbox and then made a layout and added buttons widgets to that layout(and then added that layout to the group).

All this works fine but when I then compile it, it fills the whole width of the groupbox and something like "50" in height.

I then tried to use setGeometry on the buttons but this does nothing, I can set a size with setFixedSize() but not a positions, why is this? and how can i set a position on the buttons?

Was it helpful?

Solution

You should try to add horizontal and vertical spacers in your layout to position your widget in a proper place in layout. For instance you can place two horizontal spacers on the left and right side of your button and two vertical spacers at the bottom and top. This way your button always stays at the center of layout with an appropriate size.

If you are using Qt Designer you can find them in the Spacers section and add them to your layout. If not you can use QSpacerItem like:

QSpacerItem *horizontalSpacer;
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer, 0, 0, 1, 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top