View Full Version : QMainWindow resize with correct aspect ratio

15th November 2013, 18:05
I have been struggling quite a lot on this, there seems to be no way to let user resize a top level QMainWindow mantaining the same aspect ratio, I found lot's of posts about this on the net but no one seem to have a working answer.

Thank you

15th November 2013, 19:44
So rather than try to control something which is probably under the control of the platform's windowing system (and thus out of your control), change the behaviour of the central widget in the QMainWindow. This probably means you will have to implement a custom "wrapper" widget to serve as the central widget, and make your original central widget a child of this. QMainWindow will automatically resize your wrapper widget to occupy all of the space available. However, this wrapper can make its child widget any size and put it any place it wants to. So the resizeEvent for the wrapper widget needs to compute the maximum size for your fixed aspect widget given the new size of the wrapper, and then resize and position the fixed aspect widget appropriately. You probably will need to implement the paint event for the wrapper widget so you can fill the empty background with whatever color you like.