7
votes

I am trying to place a set of buttons so that they are anchored to the bottom right of the screen. My problem is that whenever I resize the screen, the buttons are not anchored to the bottom right, but stay in its current position.

I have placed two Push Buttons inside a Horizontal Layout. I then placed this layout inside a Grid Layout, which contains a Horizontal and Vertical Spacer. I have modified the Grid Layout layoutSize property to SetMaximumSize.

What am I doing incorrectly, so that I can get my buttons to be anchored to the bottom right?

enter image description here

3

3 Answers

7
votes

You have almost everything just right here, but you probably overlooked something that is really easy to miss when you first start using Qt Designer.

Your grid layout is sitting inside your widget with a fixed size and position. It too needs to be managed by a layout. If you take a look at the Object Inspector on the top right (that contains your hierarchy) you will probably see your top level widget with a red icon. This indicates that it contains no layout. You have two options to fix this...

  1. Have your existing grid layout placed into another main layout (like a vertical layout). You would simply right click on your top level widget in the Object Inspector -> Lay Out -> [Choose a main layout type].
  2. Have your grid be the main layout. To do this you would need to remove the grid layout and have your child items arranged exactly how you have them in that picture. Then follow the previous option, right clicking on the top level widget (or the blank background) and choose Lay out -> Grid. This will pop your widgets into a Grid at a best visual fit (which you can then fix if needed), and your grid will be the top level layout.
1
votes

That grid layout will make placing other widgets quite hard. Try this instead:

  1. Add (from left to right) horizontal spacer and the two buttons.
  2. Multiselect them all.
  3. Select "Lay Out Horizontally" (Ctrl-H) from the Qt Designer's (or Qt Creator's) top toolbar (not from the widget box in the left!).
  4. Add vertical spacer on top of the previous widgets.
  5. Select the main window by clicking it (none of the added widgets are now selected).
  6. Select "Lay Out Vertically" (Ctrl-L) from the top toolbar.
  7. Done.
1
votes

It seems that you're doing it correctly. Just forgot to apply a layout to your central widget, right? The Grid layout should be arranged in your central widget. The more convenient way is to remove grid layout widget and lay out the central widget in a grid ;-)