[PyQt] Force the ratio of a widget

Doug Bell dougb at bellz.org
Wed Jan 14 14:11:08 GMT 2009


Frédéric wrote:
> 
> Hello,
> 
> I have a frame with 5 buttons in cross. I want this frame always be
> square while resizing the window. This frame is in a horizontal layout,
> and does not expand. So, it is always at its minimum width. But its
> height can increase, leading to non-square ratio.
> 
> What I would like to do is to force the square ratio, by changing the
> width as soon as the height changes. For that, I overwrited the
> resizeEvent() method with the following:
> 
> def resizeEvent(self, event):
>     size = event.size()
>     if size.height() != size.width():
>          self.resize(size.height(), size.height())
> 
> It works half the way; the frame remains square, but its parent (the
> layout) does not resize, so I  can't see the entire frame. In fact, the
> layout remains at the minimum width of the frame.
> 
> Any idea to correct this?

Calling resize inside a resizeEvent is not a good idea.  You should let
the layout do the work.

Try overriding QLayoutItem.hasHeightForWidth() and
QLayoutItem.heightForWidth(w) in a custom layout.

Doug.


More information about the PyQt mailing list