How to force layout update/resize when child it manages resizes?

The layout respects the sizeHint() and minimalSizeHint() of its children (not their size()). So you should reimplement these two virtual methods and return the size you want to force. Whenever your size hint changes, you should call updateGeometry() on your own widget to trigger an update of the layout where the widget is placed in.

See documentation:

  • virtual QSize QWidget::sizeHint() const

    This property holds the recommended size for the widget.
    [...]

  • virtual QSize QWidget::minimumSizeHint() const

    This property holds the recommended minimum size for the widget.
    [...]
    QLayout will never resize a widget to a size smaller than the minimum size hint unless minimumSize() is set or the size policy is set to QSizePolicy::Ignore. If minimumSize() is set, the minimum size hint will be ignored.

So, to force a minimum size of your widget, implement minimumSizeHint() by returning your minimum size; to force a fixed size, implement both by returning the same size.

I also recommend reading the following section in the documentation page "Layout Management":

Custom Widgets in Layouts

When you make your own widget class, you should also communicate its layout properties. If the widget has a one of Qt's layouts, this is already taken care of. If the widget does not have any child widgets, or uses manual layout, you can change the behavior of the widget using any or all of the following mechanisms:

  • Reimplement QWidget::sizeHint() to return the preferred size of the widget.
  • Reimplement QWidget::minimumSizeHint() to return the smallest size the widget can have.
  • Call QWidget::setSizePolicy() to specify the space requirements of the widget.

Call QWidget::updateGeometry() whenever the size hint, minimum size hint or size policy changes. This will cause a layout recalculation. Multiple consecutive calls to QWidget::updateGeometry() will only cause one layout recalculation.


Update (see comments):

You can also force a size (or height / width only) by calling:

QWidget::setFixedSize(QSize);
QWidget::setFixedHeight(int);
QWidget::setFixedWidth(int);

These methods simply set the size hints to the given value and call updateGeometry() AFAIK. But maybe they do a little bit more since (as seen in the comments) this was the only way I could find to solve the problem here.

Tags:

Qt