Theodore Tang
Theodore Tang

Reputation: 1021

Adjusting QMainWindow size

I am currently building a minesweeper game which has three different game modes. Harder mode has more objects and a bigger window size. When I try to switch back to a simpler mode, the size of the window will not resize to a smaller window, which will make the layout look terrible.

Before it looks like this:
enter image description here

After switching from a harder mode, the layout will be like this(which remains the size of the harder mode):
enter image description here

So what can I do to set those windows to suitable sizes? Or what can I do to set a default window size for each mode?
Thanks

Upvotes: 1

Views: 3319

Answers (1)

ypnos
ypnos

Reputation: 52407

There is a few things you should look into:

  1. Size policies. Size policies of the individual widgets dictate how they might shrink or be expanded vertically and horizontally. With the right policies, elements like the icons and scores at the bottom will stay reasonably sized in all geometric configurations. Depending on how you realized the playing field, you can also use size policies there to ensure that elements don't grow too big.

  2. Spacers. A horizontal spacer element in the middle of your bottom bar will help keeping the icons and scores small and in position. It will eat up the additional space.

  3. QWidget's adjustSize() method. You can call it whenever your playing field changes. If your playing field does not work with size hints, but absolute sizes, you can call resize() on the playing field widget, and then. if necessary, adjustSize() on the main window.

Upvotes: 1

Related Questions