[PyQt] Manually trigger resize event

Tong Zhang warriorlance at gmail.com
Wed Sep 12 14:19:10 BST 2018


Thanks Maurizio! Works with resizeEvent() method.

What I wanna do is to adapt the widget size which could be controlled by 
some other method but the layout just cannot be auto-adjusted, unless I 
maximize&restore the window (that is trigger the resize event). Now it 
works.

Tong

On 09/11/2018 11:04 PM, Maurizio Berti wrote:
> Depending on your needs, every QLayout descendent uses the methods 
> invalidate(), update() or activate() you can call for.
> Also, every widget has updateGeometry(), which is usually automatically 
> called whenever you change the return value of sizeHint() or 
> sizePolicy() (and, I suppose, this works by setting a new QSizePolicy 
> too). This works only for visible widgets, though, and it's possible 
> that child widgets for which the size isn't changed by this call won't 
> receive a resizeEvent.
> 
> Maybe you can manually call QWidget.resize() with the current size, but 
> I'm not sure that it would work everytime.
> 
> Another possibility is to manually call the resizeEvent:
> 
>      self.someWidget.resizeEvent(QtGui.QResizeEvent(newSize, oldSize))
> 
> This will trigger the resizeEvent method of the widget. You could call 
> from the widget itself, and also use an invalid size as the old size, 
> which should be enough for Qt to "invalidate" the previous size and do 
> its magic, since we're telling it that the previous (fake) was different:
> 
>      self.resizeEvent(QtGui.QResizeEvent(self.size(), QtCore.QSize()))
> 
> You should remember that you might also need to call the class 
> resizeEvent method if you are overriding it, because some child widgets 
> need its notification.
> 
> Be aware, anyway, that resizeEvent is called always when it's really 
> necessary (and Qt takes good care to do so), so be sure that you are not 
> implementing something in the wrong way. If you tell us what you're 
> needing it for, we could help you with that.
> 
> Maurizio
> 
> 
> 2018-09-11 23:20 GMT+02:00 Tong Zhang <warriorlance at gmail.com 
> <mailto:warriorlance at gmail.com>>:
> 
>     Hello,
> 
>     When building PyQt application, sometimes I want to manually trigger
>     the resize event, so as to fit the widgets, how can I do that?
> 
>     Any comment would be appreciated!
> 
>     Tong
> 
>     _______________________________________________
>     PyQt mailing list PyQt at riverbankcomputing.com
>     <mailto:PyQt at riverbankcomputing.com>
>     https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>     <https://www.riverbankcomputing.com/mailman/listinfo/pyqt>
> 
> 
> 
> 
> -- 
> È difficile avere una convinzione precisa quando si parla delle ragioni 
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net


More information about the PyQt mailing list