PDA

View Full Version : Set minimum size of a container without distorting its children



alex_sh
10th February 2019, 19:10
Hi,

I have a QWidget (QFrame) container with a few widgets inside (usually QTableView, Qwt plot, etc...). The contents of this container may change dynamically.

Generally, the container's minimum size is properly auto-calculated according to its contents using the layouts system, which helps avoiding distortion of its child widgets.

However, I want this container's size to be at least 200x200. That is, the container's minimum size should be:
max(200x200, contents_size).

This is where it becomes problematic. If I use
container->setMinimumSize(200, 200) and the total size of the contents is greater than 200x200, this minimumSize allows the container to be resized to 200x200 which may distort or chop off its children.

I'm attaching an example 13024 which shows the problem - QFrame with minimumSize of 100x100 allows the child buttons to get distorted. In reality, I want the minimum size to be "at least 100x100, but more if needed".

Is there a way to fix this? I'm using Qt 5.9.

Thanks!

anda_skoa
10th February 2019, 22:57
You can try this:

1) Create a subclass of QFrame
2) overwrite minimumSizeHint() such that it takes the value from the base class and expands it.
Something like


QSize MyFrame::minimumSizeHint() const
{
const QSize baseSize = QFrame::minimumSizeHint();

return baseSize.expandedTo(QSize(200, 200));
}


Cheers,
_

alex_sh
11th February 2019, 18:48
You can try this:

1) Create a subclass of QFrame
2) overwrite minimumSizeHint() such that it takes the value from the base class and expands it.
_


Thanks, that would work I think.

I kinda wanted to do it in Qt Designer without reverting to subclassing and stuff, but I guess there's no other way...

It's quite strange that Qt doesn't provide this facility out of the box though.

Thanks!
Alex

anda_skoa
13th February 2019, 21:36
I kinda wanted to do it in Qt Designer


You can still use Designer:
1) Add a frame
2) Use the "promote widget" feature to replace it with your frame subclass when the code gets generated by UIC.

Cheers,
_