Reduce the size of the parent widget after resizing the child

How can I make all widgets shrink if one of their children changes height?
They expand if the new height is greater, but do not decrease if they are smaller.

I tried updateGeometry(), but didn’t change anything.

Here is an example, after the child element shran, the height of the parents does not change:
enter image description here

import sys
from PyQt4.QtGui import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        # inner widget
        self.widget = QGroupBox('inner')
        vbox_inner = QVBoxLayout(self.widget)

        button1 = QPushButton('100')
        button1.clicked.connect(lambda: self.change_height(100))
        vbox_inner.addWidget(button1)
        button2 = QPushButton('200')
        button2.clicked.connect(lambda: self.change_height(200))
        vbox_inner.addWidget(button2)

        # outer layout
        vbox = QVBoxLayout(self)
        vbox.addWidget(self.widget)

    # resizing element
    def change_height(self, height):
        self.widget.setFixedHeight(height)


app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())

Is there a way to customize all parent widgets?

answer in Python:

widget = self.parent()
while widget:
    widget.adjustSize()
    widget = widget.parent()
+3
source share
1 answer

You must call the method of adjustSize()your widget "toplevel" (yours MainWindowin this case).

, . ++ - :

QWidget *w = <the widget you resized>->parentWidget();
while (w) {
  w->adjustSize();
  w = w->parentWidget();
}
+7

All Articles