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.
Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.
The tree-like organization of QWidgets (and in fact of any QObjects ) is part of the memory management strategy used within the Qt-Framework. Assigning a QObject to a parent means that ownership of the child object is transferred to the parent object. If the parent is deleted, all its children are also deleted.
The QWidget class is the base class of all user interface objects.
Detailed Description. Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu, or by clicking the X title bar button. They are also sent when you call QWidget::close() to close a widget programmatically.
You can derive from widget
class and reimplement resizeEvent
event
If you have any other QObject that can have strict relation to that QWidget you may use QObject::installEventFilter(QObject * filter)
and overload bool eventFilter(QObject *, QEvent *)
. See more at Qt docs
In case you are using Python with PyQt4, you can set widget.resizeEvent
to your function without sublclassing it:
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
def onResize(event):
print event
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
widget = QtGui.QPushButton('Test')
widget.resizeEvent = onResize
widget.resize(640, 480)
widget.show()
sys.exit(app.exec_())
Sorry, it looks like a hack, but I use this:
some_widget.resizeEvent = (lambda old_method: (lambda event: (self._on_resized(event), old_method(event))[-1]))(some_widget.resizeEvent)
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:
class TransparentOverlay(QtGui.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
self._updateParent(self.parentWidget())
def setParent(self, parent, *args):
prevParent = self.parentWidget()
super().setParent(parent, *args)
self._updateParent(parent, prevParent)
def unsetParent(self, parent=None):
if parent is None:
parent = self.parentWidget()
if parent is not None and hasattr(parent.resizeEvent, '_original'):
parent.resizeEvent = parent.resizeEvent._original
def _updateParent(self, parent, prevParent=None):
if parent is not prevParent:
self.unsetParent(prevParent)
if parent is not None:
original = parent.resizeEvent
def resizeEventWrapper(event):
original(event)
self.resize(event.size())
resizeEventWrapper._original = original
parent.resizeEvent = resizeEventWrapper
self.resize(parent.size())
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 the QGraphicsView
itself.
You can override the resizeEvent
by
def resizeEvent(self, newSize):
#do code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With