[PyQt] Manually trigger resize event

Maurizio Berti maurizio.berti at gmail.com
Wed Sep 12 04:04:26 BST 2018

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

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.


2018-09-11 23:20 GMT+02:00 Tong Zhang <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
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180912/4aecd942/attachment.html>

More information about the PyQt mailing list