Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain widgets aspect ratio in Qt?

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

like image 540
Bleadof Avatar asked Jan 16 '09 22:01

Bleadof


People also ask

How do I resize a layout in Qt?

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.

How do you overlap widgets in Qt?

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.

How do I create a custom widget in Qt?

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'.


2 Answers

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.

like image 86
Marc Mutz - mmutz Avatar answered Nov 03 '22 14:11

Marc Mutz - mmutz


The right answer is to create your custom layout manager. That is possible by subclassing QLayout.

Methods to implement when subclassing QLayout

void addItem(QLayoutItem* item);
Adds item to layout.
int count() const;
Returns the item count.
QLayoutItem* itemAt(int index) const;
Returns item reference at index or 0 if there's none.
QLayoutItem* takeAt(int index);
Takes and returns item from the layout from index or returns 0 if there is none.
Qt::Orientations expandingDirections() const;
Returns the layouts expanding directions.
bool hasHeightForWidth() const;
Tells if the layout handles height for width calculation.
QSize minimumSize() const;
Returns the layouts minimum size.
void setGeometry(const QRect& rect);
Sets the geometry of the layout and the items inside it. Here you have to maintain the aspect ratio and do the centering.
QSize sizeHint() const;
Returns the preferred size for the layout.

Further reading

  • Maintaining square form for a widget in Qt @ Forum Nokia
  • Implementing a layout manager in Qt @ Forum Nokia
  • Writing custom layout managers @ Qt documentation
like image 38
Bleadof Avatar answered Nov 03 '22 14:11

Bleadof