Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWidget's minimumSize vs minimumSizeHint

Tags:

layout

qt

qwidget

Qt's widgets have 2 properties determining how small they can be:

  • minimumSizeHint
  • minimumSize

Now their basic difference seems clear enough: minimumSize restricts what size the widget can actually have, and Qt won't allow setting the size smaller. It also overrides minimumSizeHint, if set. And then minimumSizeHint is only used by layouts, which won't resize the widget smaller than that when resizing its contents.

Now I was implementing a custom widget, no problems there, and I started wondering about this.

Question: why would you ever want to use mininumSizeHint at all? What is the scenario, where you have to, or want to use it instead of just always using minimumSize? Why does minimumSizeHint exist?

(And if answer is "yeah, sizeHint is enough for everything", that's fine.)

like image 832
hyde Avatar asked Aug 11 '26 17:08

hyde


1 Answers

I think the main difference is that minimumSize can be changed by any other object at any time, whether you like it or not, as all QWidgets have a public setMinimumSize() function; whereas your widget alone controls what is returned by minimumSizeHint().

As an implementer of a custom widget, you always want to override minimumSizeHint() if your widget has a minimum size that you think should usually be respected. This is what the built-in Qt widgets do.

like image 57
RyanCu Avatar answered Aug 14 '26 08:08

RyanCu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!