Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWidget resize signal?

Tags:

qt

qt4

pyqt

pyqt4

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.

like image 530
warvariuc Avatar asked Jan 21 '12 19:01

warvariuc


People also ask

What is QWidget in Qt?

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.

What is QWidget * parent?

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.

What is QWidget class window?

The QWidget class is the base class of all user interface objects.

How do I turn off QWidget?

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.


6 Answers

You can derive from widget class and reimplement resizeEvent event

like image 84
Lol4t0 Avatar answered Sep 20 '22 10:09

Lol4t0


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

like image 28
Kamil Klimek Avatar answered Sep 22 '22 10:09

Kamil Klimek


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_())
like image 42
reclosedev Avatar answered Sep 23 '22 10:09

reclosedev


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)
like image 45
Vladyslav Savchenko Avatar answered Sep 20 '22 10:09

Vladyslav Savchenko


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:

  • The original method is stashed in the _original attribute of the new one. This is possible because functions are objects.
  • The new method truly subclasses any 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.

like image 28
Mad Physicist Avatar answered Sep 19 '22 10:09

Mad Physicist


You can override the resizeEvent by

def resizeEvent(self, newSize):
    #do code here
like image 39
chicken-me Avatar answered Sep 23 '22 10:09

chicken-me