[Interest] heightForWidth

John Weeks john at wavemetrics.com
Fri Mar 7 01:59:44 CET 2014


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?

Thank you all!

-John Weeks




More information about the Interest mailing list