How is it possible to maintain widgets aspect ratio in Qt and what about centering the widget?
Once you have add your layout with at least one widget in it, select your window and click the "Update" button of QtDesigner. The interface will be resized at the most optimized size and your layout will fit the whole window. Then when resizing the window, the layout will be resized in the same way.
Right click on the root widget (easiest in the Object Inspector), and from the bottom of context menu, select Lay out > - Lay out in Grid. Right click on the label, and from Layout alignment > set it aligned to the corner you want.
Adding the Custom Widget to Qt Designer. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. Click New Widget so that we are ready to add our new widget. Change the Class name from 'MyCustomWidget' to 'Vcr'.
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.
The right answer is to create your custom layout manager. That is possible by subclassing QLayout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With