[Interest] heightForWidth

Bo Thorsen bthorsen at ics.com
Fri Mar 7 09:14:59 CET 2014


Den 07-03-2014 01:59, John Weeks skrev:
> I have a need for windows that maintain a set aspect ratio, so I implemented heightForWidth(). Since I'm still exploring, I made it very simple (this function is called by a wrapper class that actually implements heightForWidth()):
>
>   int grafRec::doHeightForWidth(int w)
> {
> 	return w/2;
> }
>
> I set a size policy:
>
>   		QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
> 		policy.setHeightForWidth(true);
> 		content->setSizePolicy(policy);
>
> The object called "content" is a subclass of QWidget and it is embedded in a cell of a QGridLayout. There are no other cells in the layout occupied. The layout and managed content widget are embedded in a top-level widget.
>
> This actually works up to a point. As I make the window get wider, it also gets taller in proportion. But evidently Qt is enforcing a height that is *at least* half the width, where I really need a height that is *exactly* half the width.
>
> Is this working as expected, that the height returned by heightForWidth() is really a minimum height? If that is so, is there a way to achieve what I really want?

Hi John,

It sounds like you don't have any other widget that will take up the 
space in the layout? The layout managers will only use the height for 
width as a hint, so you have to provide some way to help it. Either add 
other widgets to take the empty space around the widgets or add spacers.

I hope this helps,

Bo.

-- 
Bo Thorsen, European Engineering Manager, ICS
Meet me at our quickstarts across Europe in March.
Copenhagen, Hamburg, Munich and Zurich.
See this page: http://ics.com/qt-quickstart



More information about the Interest mailing list