Reputation: 13
How can I lock the aspect-ratio for resizing the window of my application?
For Example aspect-ratio: 16/9.
Upvotes: 1
Views: 169
Reputation: 73101
If you mean you want to be prevent the user from resizing the away from a fixed 16:9 ratio (so e.g. if the user drags the window shorter, it would automatically become thinner as well), I'm not sure that's possible via the Qt API, because window-resizing is handled by the OS's window manager, not by the Qt library itself. Qt only gets notified after the resize-step has already happened. There might be some lower-level API that would constrain the window's aspect ratio to be constant, but that would be OS-specific, not part of Qt.
One work-around would be to make the window fixed-size (by calling QWidget::setFixedSize()) so that the user can't resize it by dragging on the edges or corners, and then provide some other resizing mechanism (e.g. "larger" and "smaller" buttons) that the user could activate to resize the window.
Another possible work-around that you could do at the Qt level is to react to a resizeEvent()
by calculating the largest 16:9 sub-rectangle that will fit inside the new window-size, and layout your content to fit inside that rectangle instead of filling the entire window. That would result in bars of empty space at the top and bottom (or left and right) edges of your window whenever the window wasn't at a 16:9 aspect ratio, but at least your content could still have the desired aspect ratio regardless of how the user resizes the window.
Upvotes: 1