PDA

View Full Version : Widget with fixed aspect ratio



^NyAw^
19th October 2015, 10:57
Hi,

I'm trying to get two QWidget derived classes that need to mantain a "forced" aspect ratio.

The first widget class must be "square" as I need to display a circle inside it.

The second widget will contain a set of this first widget class with some rows and some columns.

On the attached image you can see what I want to achieve.11455

The first widget class is the widget that displays a green circle with a text inside. The second widget is the "container" widget that includes the set of this first class widget into rows and columns. There is also a widget that the only thing that it do is to get space and so I can get "displacement" as I need the fist class widgets to be disposed in the way that you can see on the attached image.

Can someone point me on how to achieve this?

What I have tryied is to create a QGridLayout derived class that reimplements "hasHeightForWidth" and "heightForWidth" with no luck.


Thanks,

Kryzon
19th October 2015, 21:11
You need to post your code, just to eliminate the chance that it's something obvious that you're missing.

Note that QWidget also has these functions. Can you give them a try?
http://doc.qt.io/qt-5/qwidget.html#hasHeightForWidth

EDIT: actually, I think the layout of the container will be querying this from the layout items. 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.
The default implementation of the layout items ignores the setting:
http://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/kernel/qlayoutitem.cpp#n368

You would use this just for the circle widgets. The container widget will be sized according to its contents.
For the empty spaces, the layouts let you add spacers:
http://doc.qt.io/qt-5/qspaceritem.html#details