How to maintain widgets aspect ratio in Qt?

qt widget fixed aspect ratio
pyqt aspect ratio
qt maintain aspect ratio
qsizepolicy

How is it possible to maintain widgets aspect ratio in Qt and what about centering the widget?


You don't have to implement your own layout manager. You can do with inheriting QWidget and reimplementing

int QWidget::heightForWidth( int w ) { return w; }

to stay square. However, heightForWidth() doesn't work on toplevel windows on X11, since apparently the X11 protocol doesn't support that. As for centering, you can pass Qt::AlignCenter as the third parameter of QBoxLayout::addWidget() or the fifth parameter of QGridLayout::addWidget().

Note: In newer versions of Qt at least, QWidget does not have the heightForWidth or widthForHeight anymore (so they cannot be overriden), and therefore setWidthForHeight(true) or setHeightForWidth(true) only have an effect for descendants of QGraphicsLayout.

How to maintain widgets aspect ratio in Qt?, You don't have to implement your own layout manager. You can do with inheriting QWidget and reimplementing int QWidget::heightForWidth(  How to maintain widgets aspect ratio in Qt? 4 answers I want to keep an aspect ratio of 16/9 on a QGroupBox (the one on the left on the screenshot below). For the tests I made a simple program with 4 groupboxes in a vertical layout, all in an horizontal layout:


[Solved] How to Preserve the aspect ratio of a widget?, i.e.: Change the height of the QGroupBox programmaticaly to keep the same aspect ratio. I read that the right way of doing this is to use my own custom layout for the layout were QGroupBox is going to be attached, re-implement the function "int heightForWidth(int) const" and use QGroupBox::sizePolicy(). QSizePolicy::Preferred is the most general one and actually lets the layout almost do what it wants. @raven-worx said in Maintain a Square aspect ratio on widget resizing: play around with the size policy values. QSizePolicy::Preferred is the most general one and actually lets the layout almost do what it wants.


Calling resize() from within resizeEvent() has never worked well for me -- at best it will cause flickering as the window is resized twice (as you have), at worst an infinite loop.

I think the "correct" way to maintain a fixed aspect ratio is to create a custom layout. You'll have to override just two methods, QLayoutItem::hasHeightForWidth() and QLayoutItem::heightForWidth().

Widget with fixed aspect ratio, So you should subclass QLayoutItem and use QLayout::addItem or QBoxLayout::insertItem etc. to add your QLayoutItem derived objects that return 'true' for that ratio function, and the height etc. You would use this just for the circle widgets. The container widget will be sized according to its contents. You can't have view, add dummyLayout into it and then add view into dummyLayout. You need something like placeholderWidget->aspectRatioLayout->view. placeHolder widget is empty resizeable widget, e.g. from Qt Designer. view is your widget that will have fixed aspect ratio, e.g. image, live camera view,


In my case overriding heightForWidth() doesn't work. And, for someone, it could be helpful to get working example of using resize event.

At first subclass qObject to create filter. More about event filters.

class FilterObject:public QObject{
public:
    QWidget *target = nullptr;//it holds a pointer to target object
    int goalHeight=0;
    FilterObject(QObject *parent=nullptr):QObject(parent){}//uses QObject constructor
    bool eventFilter(QObject *watched, QEvent *event) override;//and overrides eventFilter function
};

Then eventFilter function. It's code should be defined outside of FilterObject definition to prevent warning. Thanks to this answer.

bool FilterObject::eventFilter(QObject *watched, QEvent *event) {
    if(watched!=target){//checks for correct target object.
        return false;
    }
    if(event->type()!=QEvent::Resize){//and correct event
        return false;
    }

    QResizeEvent *resEvent = static_cast<QResizeEvent*>(event);//then sets correct event type

    goalHeight = 7*resEvent->size().width()/16;//calculates height, 7/16 of width in my case
    if(target->height()!=goalHeight){
        target->setFixedHeight(goalHeight);
    }

    return true;
};

And then in main code create FilterObject and set it as EventFilter listener to target object. Thanks to this answer.

FilterObject *filter = new FilterObject();
QWidget *targetWidget = new QWidget();//let it be target object
filter->target=targetWidget;
targetWidget->installEventFilter(filter);

Now filter will receive all targetWidget's events and set correct height at resize event.

QLayout that keeps fixed aspect ratio of it's only widget. · GitHub, \brief Layout that keeps single item and maintains given aspect ratio Qt::​Orientations expandingDirections() const override. {. // we'd like grow beyond sizeHint  Comment est-il possible de maintenir le format des widgets dans Qt et qu'en est-il du centrage du widget? 31. qt user-interface widget aspect-ratio.


PyQt/Creating a widget with a fixed aspect ratio, One place to look for information is the Qt Quarterly article, Trading Height for Width which shows how this is done using C++ and Qt 3. Another  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.


[Qt-interest] Aspect Ratio with QWidget::heightForWidth(int), void MainWindow::resizeSessionWidget(void) { double ratio = 1.4; .com/​questions/452333/how-to-maintain-widgets-aspect-ratio-in-qt But it  Creating a widget with a fixed aspect ratio. On the #pyqt channel on Freenode, magicblaze007 asked for an example of a custom widget with a fixed aspect ratio. One place to look for information is the Qt Quarterly article, Trading Height for Width which shows how this is done using C++ and Qt 3. Another example is shown here.


user interface - How to maintain widgets aspect ratio in Qt?, I think the "correct" way to maintain a fixed aspect ratio is to create a custom layout. You'll have to override just two methods, QLayoutItem::hasHeightForWidth​()  The following QLabel-based widget that will preserve the aspect ratio of a pixmap assigned to it. It uses the heighForWidth method to return the preferred height of the widget given its width. This way, the widget naturally respects the aspect ratio of the pixmap when resized and scales it accordingly. Tested in PyQt5.