I want to take an action when a widget was resized.
Is there a way to catch that without installing an event filter on that widget (and, obviously, without subclassing it)? AFAIK, QWidget does not have a resized
signal.
I want to take an action when a widget was resized.
Is there a way to catch that without installing an event filter on that widget (and, obviously, without subclassing it)? AFAIK, QWidget does not have a resized
signal.
You can derive from
widget
class and reimplementresizeEvent
eventYou can override the
resizeEvent
byIf you have any other QObject that can have strict relation to that QWidget you may use
QObject::installEventFilter(QObject * filter)
and overloadbool eventFilter(QObject *, QEvent *)
. See more at Qt docsIn case you are using Python with PyQt4, you can set
widget.resizeEvent
to your function without sublclassing it:Sorry, it looks like a hack, but I use this:
This is a couple of years too late, but I was working on a transparent overlay widget that would completely cover the parent. You can not do what you want without subclassing, but you can restrict the subclassing to an instance as @reclosedev suggests, meaning that you don't have to actually create a subclass.
I wrote the following snippet (which works in PyQt4) for following the size of any widget that the widget is added to:
This code uses a couple of neat tricks that are possible with Python:
_original
attribute of the new one. This is possible because functions are objects.QWidget
instance, meaning that you do not have to create an actual subclass. Each parent instance will effectively become an instance of a subclass by virtue of the tacked on method.If you need a one-time thing, all of the code for removing the subclassed
resizeEvent
method and replacing it with the original can be trashed. In that case, the solution is basically a fancier version of @reclosedev's solution, but with @Chris's comments about preserving the original addressed.The only caveat with this code is that it does not support GL widgets correctly, so for example the overlay can not always be added to the viewport of a
QGraphicsView
. It can, however, be added to theQGraphicsView
itself.