Here's my test code:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
visible: true
readonly property int defaultWidth: 700
readonly property int defaultHeight: 500
width: defaultWidth
height: defaultHeight
readonly property real aspectRatio: defaultWidth / defaultHeight
readonly property real scaleFactor: width / defaultWidth
onWidthChanged: {
var properHeight = Math.round(width / aspectRatio);
if ( height !== properHeight ) {
height = properHeight
}
}
onHeightChanged: {
var properWidth = Math.round(height * aspectRatio);
if ( width !== properWidth ) {
width = properWidth
}
}
TextEdit {
id: textEdit
text: "Lorem ipsum"
font.pixelSize: 40 * scaleFactor
anchors.fill: parent
}
}
It implements fixed aspect ratio, but it doesn't work very well: there is sort of flicker (window size jumping between the fixed-aspect-ratio size and the mouse-resized size), and it may adjust wrong dimension depending on order of width and height change events. In effect, when size is adjusted programmatically, the window edge may start jumping between mouse cursor position and programmatically set, different position.
What should happen is, the window edge which is dragged should follow mouse cursor (like in any resize), and the other edges should change to maintain aspect ratio:
- If user drags left/right edge, bottom edge should move to maintain aspect ratio.
- If user drags top/bottom edge, then right edge should also move to maintain aspect ratio.
- When user drags a corner, there are two possible sizes to take to keep the aspect ratio, as either height or width could be changed programmatically. How exactly to determine this doesn't matter, as long as it is consistent.
- It is ok, if aspect ratio is fixed only after resizing stops, though real-time would be better. But how to reliably detect when resizing actually stops?
- If this can't be done with Qt alone, I'm interested in both Windows and X11/Linux specific solutions.
How to achieve the resize while preserving aspect ratio, using native window resize controls (normal window borders or whatever the platform uses), so that it behaves logically and looks nice ?
Edit, "hint": I had time to do some research, overriding QQuickView::resizeEvent and calling resize from there seems to be able to do the trick, but I didn't have time to get it to work quite right yet.