1 Attachment(s)
Set minimum size of a container without distorting its children
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
Code:
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 Attachment 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!
Re: Set minimum size of a container without distorting its children
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
Code:
QSize MyFrame
::minimumSizeHint() const {
return baseSize.
expandedTo(QSize(200,
200));
}
Cheers,
_
Re: Set minimum size of a container without distorting its children
Quote:
Originally Posted by
anda_skoa
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
Re: Set minimum size of a container without distorting its children
Quote:
Originally Posted by
alex_sh
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,
_