View Full Version : Widget with fixed aspect ratio

19th October 2015, 10:57

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.


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?

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:

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: