I'm creating a new widget, by subclassing the QWidget class. I'd like to be able to set a ratio (for its height and its width) for this widget, which will always be maintained.
For this, I've always searched, using the Qt5 documentation, Google, and Stackoverflow. Obviously, I've found answers: in particular, this one. But, unfortunately, not even one is fully effective:
- Setting the
sizeIncrement
does totally nothing, even if the widget is a window - I tried to overload resizeEvent, but I really don't know how to do this...
If I follow this answer, two things:
- If the widget is a top-level window, the ratio isn't maintained at all, I can resize it as I want.
- If I place this widget in a layout, if I just increase both width and height of the window, the ratio is maintained. But as soon as I increase the width or the height to much, the widget is flattened. Instead, I would like that the layout automatically adjust its size to keep the widget's ratio.
So, how could I manage to keep the aspect ratio of a subclassed QWidget?
QLabel
s. #8212482 – ManellaQWidget::heightForWidth()
? That allows you to specify an aspect ratio. If that's not enough, create a parent "dummy" QWidget to hold your widget in a 3x3 QGridLayout, with your widget in the middle and QSpacerItem's around the edges (seeQGridLayout::addItem()
). The QSpacerItem's can provide the extra spacing (or be sized down to 0) as needed when resizing. You'd probably need to useQGridLayout::setColumnStretch()
andQGridLayout::setRowStretch()
. – Talamantes