Important: Please read the Qt Code of Conduct -

Maintaining fixed aspect ratio for a widget

  • There seems to be a fair amount written on this topic, but none of it quite clarifies my problem: I have a widget in a sub-window that I want to keep a fixed aspect ratio. The widget uses a custom layout class that inherits QHBoxLayout and re-implements heightForWidth and hasHeightForWidth. To simplify testing, heightForWidth returns a value equal to its parameter (producing a square). In the widget's size policy, heightForWidth is set to "true." Since I've made these changes, the window continues to resize without restriction, but the internal widget is frozen as a small square, anchored to the middle left-hand side of the window. What am I missing, here?

  • When you add the widget to your layout, precise its stretch:
    @hLayout->addWidget( widget, 1 );@

  • No effect, I'm afraid. I wonder, is there anything I need to re-implement for the mouse?

  • I finally gave up on heightForWidth and am now trying to accomplish this with QResizeEvent. If I re-implement it for the widget, it only controls the resizing of the contents of the sub-window, not the sub-window's frame. When I do it for the sub-window (i.e., in a class inheriting QMdiSubWindow), it has no effect! Someone, please throw me a line; I'm running out of ideas....

    [EDIT -- After more testing, I realized that I've got a pointer problem: QMdiArea expects type QMdiSubWindow, so I used an unsafe cast to get it to accept my new class. That means I have a base-class pointer directed at an instance of a derived class, which I think explains why my resize code isn't executing. But I'm not sure how I can get around this. Do I have to re-impement QMdiArea as well?]

Log in to reply