Flone
Flone

Reputation: 317

How to change the size of child widget when the parent size changes?

I have 2 widgets, second widget is child of first one. I thought that if you change the size of the parent widget, the child widget will also change its size, but it's not. I searched the internet for a solution to my problem, but maybe I did not correctly formulate my question when looking for a solution. I did not find a solution and therefore wrote here.

What should I do to make child widget change its size when the parent is resized?

Upvotes: 1

Views: 5588

Answers (2)

scopchanov
scopchanov

Reputation: 8399

Cause

To automaticaly respond to the size changes of its parent widget, a child widget has to be added to its layout.

Solution

Since you do not want to use automatic layout, you have to resize the child widget manually. For that purpose reimplement QWidget::resizeEvent in your custom widget.

Example

Provided you have a pointer to your child widget m_child and this child is already positioned and resized, e.g. something like that:

m_child->move(10, 20);
m_child->resize(60, 40);

a possible implementation of the resizeEvent, preserving the margins, i.e. the distance between the edges of the child and the edges of the parent, could be the following:

void ParentWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);

    int oldWidth = event->oldSize().width();

    if (oldWidth >= 0)
        m_child->resize(m_child->width() + event->size().width() - oldWidth,
                        m_child->height() + event->size().height() - event->oldSize().height());
}

The full code of the example I have prepared for you is available on GitHub.

Note: The reimplementation of the QWidget::paintEvent in the example is purely for demonstration purposes.

Upvotes: 3

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

Well, the no-layouts solution is quite obvious: override resizeEvent method in your parent and drive the children resizing from there.

On the other hand, keeping the child widget in the parent widget layout would grant it would resize along with its parent automatically, but your issue is: how to set the child widget position to a coordinate of choice?

I suggest a technique.

Say you want the child to move horizontally. Give the parent a QHBoxLayout layout (set the layout margins and spacing properties to 0), and add the child to it, so it will take the whole space, horizontally. Then add a QWidget to the same layout, at the child left: this widget will act as a filler. If you want the child x be 42, set the filler width to 42, using its setFixedWidth method, programmatically, or set its minimum and maximum width to 42 from designer.

Obviously, if you have many children, and they can move anywhere (and overlap, maybe) drop layouts altogether and resort to my very first suggestion, because, generally speaking, layouts are intended to keep things anchored to each others, and maybe that's not the case.

Upvotes: 1

Related Questions