0

I have a QWidget container (actually a QFrame) with a few widgets inside. The contents of this container (the number and/or size of the child widgets) may change dynamically. The container itself is in a scrollarea.

Generally, the container is properly resized (grown or shrunk) according to its contents. This works.

However, I also need to set a minimum size on this container so it doesn't shrink too much. That is, the container's minimum size should be: max(my_minimum_size, contents_size). This is where it becomes problematic.

If I use container->setMinimumSize(200, 200); and the size of the contents is greater than (200, 200), the container can still be resized to (200, 200) (unlike before), with its contents size ignored and being distorted or chopped off.

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

Update: This Qt Designer UI file shows exactly what I mean. There is a minimumSize set on a container, and resizing it distorts the buttons.

Alexander
  • 692
  • 6
  • 17
  • This is likely the answer you're looking for: https://stackoverflow.com/a/21265287/1329652 – Kuba hasn't forgotten Monica Feb 07 '19 at 16:29
  • @KubaOber Thanks, but I don't think it is. Plus, I don't want to limit the container's maximum size at all. In fact, I want it to grow as much as possible. – Alexander Feb 07 '19 at 17:22
  • Nothing in that answer limits the container size. In fact, the problem solved there is how to make sure that the container is always as big as it needs to be, and no bigger, and how to pass this to the scroll view. Try the example, it works. – Kuba hasn't forgotten Monica Feb 07 '19 at 17:52
  • @KubaOber "and no bigger" - that's the thing, I want it to grow as much as possible. I only wish to limit it so that it is always at least some (w,h) size, while not distorting its children. You use QLayout::SetMinAndMaxSize, which limits its growth. You also never specify any defined size, and I need that 200x200! – Alexander Feb 07 '19 at 18:19
  • Since the container is in the scrollarea, it must have a definite size: it can’t grow arbitrarily large. There’s no concept of “as large as possible” in a scroll area: things can be as large as you wish, subject to limitations of the widget system (2^16-1 limit?). So before we go any further, you must specify what you mean by “grow as much as possible”. – Kuba hasn't forgotten Monica Feb 08 '19 at 13:53
  • I really wish you tried the example though. The [`SetMinAndMaxSize`](https://doc.qt.io/qt-5/qlayout.html#SizeConstraint-enum) constraint doesn’t limit growth inherently. It only limits growth past the point of distortion, i.e. widget maximum sizes are obeyed. If widgets themselves don’t have limits, the container won’t be constrained in growth and then it’s up to you to make it bigger if you wish – and its minimum size can be trivially set by `setMinimumSize`: this size is union-ed with the minimum size of the layout (no distortion!), and the result governs the behavior of the widget. Easy. – Kuba hasn't forgotten Monica Feb 08 '19 at 13:58
  • In fact, the `SetMinAndMaxSize` policy means precisely “no distortion”, where distortion is when the widgets are sized too small (under their minimum size) or too big (over their maximum size). – Kuba hasn't forgotten Monica Feb 08 '19 at 14:00
  • @KubaOber I tried SetMinAndMaxSize and it didn't help at all. All it did was override my own minimumSize property value. I attached an example .ui file to the original post so you can see for yourself. – Alexander Feb 08 '19 at 17:38
  • I misspoke: you don't want to `setMinimumSize`, you want to reimplement `QWidget::minimumSizeHint() const override { const QSize min(200,200); return layout() ? layout()->minimumSize().expandedTo(min) : min; }`. Then you need to set the size constraint, and **then** you need to teach the scrollview how to manage the widget's size. I still don't know who resizes what: normally, a widget in a `QScrollArea` manages its size by itself, there's nothing external to resize it. What do you want to do? I can't read your mind, sorry. This question is deficient until you tell us what you want to do! – Kuba hasn't forgotten Monica Feb 08 '19 at 20:08
  • Since the container's layout automatically manages the size, where does this `200,200` arbitrary size come from? Do you want some particular widgets inside the container not to get too small? Then set their minimum sizes correctly, and the layout will enforce this; and if they are your own widgets, then most likely you didn't properly implement them (e.g. `minimumSizeHint` override). – Kuba hasn't forgotten Monica Feb 08 '19 at 20:10
  • That is exactly my requirement - I need the container to be at least my pre-defined size (200x200). Like I said, the container's minimum size must be `max(200x200, size_needed_for_children)`. They are not my own widgets, but they may be QTableViews or Qwt plots or something like that, widgets that I can't set minimumSizeHint for. – Alexander Feb 08 '19 at 21:07
  • Widgets that don't produce a proper minimum size hint are pretty much broken, so you may wish to either wrap them to fix that, or just edit the library you're using and add it to your project. It's much easier that way.\ – Kuba hasn't forgotten Monica Feb 11 '19 at 13:45

1 Answers1

0

I got my answer at Qt Centre:

https://www.qtcentre.org/threads/70015-Set-minimum-size-of-a-container-without-distorting-its-children

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

Something like:

QSize MyFrame::minimumSizeHint() const override
{
    const QSize baseSize = QFrame::minimumSizeHint();
    return baseSize.expandedTo(QSize(200, 200));
}
Alexander
  • 692
  • 6
  • 17