简体   繁体   English

有人可以解释 QT 中的尺寸提示、尺寸策略、尺寸限制吗?

[英]Can someone explain size hint, size policy, size constraint in QT?

Can anyone give a clear explain of these 3 concept?任何人都可以清楚地解释这三个概念吗? What's the difference and how to use them?有什么区别以及如何使用它们?

size hint is the preferred size of the widget, layouts will try to keep it as close to this as possible.大小提示是小部件的首选大小,布局将尝试使其尽可能接近此大小。

size policy describes how the size may change when the preferred size cannot be used (can it stretch or shrink) see the QSizePolicy::Policy enum for a description of each.大小策略描述了当首选大小无法使用时大小如何改变(可以拉伸或收缩),请参阅QSizePolicy::Policy枚举以获取每个的描述。

size constraint are the maximumSize and minimumSize the widget can be.尺寸约束是小部件可以是的最大尺寸和最小尺寸。

It puzzles me a lot too until I understand the Layout Management of Qt.在我了解 Qt 的布局管理之前,它也让我很困惑。 According to its docs :根据其 文档

When you add widgets to a layout, the layout process works as follows:向布局添加小部件时,布局过程如下:

  1. All the widgets will initially be allocated an amount of space in accordance with their QWidget::sizePolicy() and QWidget::sizeHint().所有小部件最初将根据它们的 QWidget::sizePolicy() 和 QWidget::sizeHint() 分配一定数量的空间。

  2. If any of the widgets have stretch factors set, with a value greater than zero, then they are allocated space in proportion to their stretch factor (explained below).如果任何小部件设置了拉伸因子,并且其值大于零,那么它们将按其拉伸因子的比例分配空间(如下所述)。

  3. If any of the widgets have stretch factors set to zero they will only get more space if no other widgets want the space.如果任何小部件的拉伸系数设置为零,它们只会在没有其他小部件需要空间的情况下获得更多空间。 Of these, space is allocated to widgets with an Expanding size policy first.其中,空间首先分配给具有扩展大小策略的小部件。

  4. Any widgets that are allocated less space than their minimum size (or minimum size hint if no minimum size is specified) are allocated this minimum size they require.任何分配的空间小于其最小尺寸(如果未指定最小尺寸,则为最小尺寸提示)的任何小部件都被分配到它们所需的最小尺寸。 (Widgets don't have to have a minimum size or minimum size hint in which case the stretch factor is their determining factor.) (小部件不必具有最小尺寸或最小尺寸提示,在这种情况下,拉伸因子是它们的决定因素。)

  5. Any widgets that are allocated more space than their maximum size are allocated the maximum size space they require.任何分配的空间大于其最大尺寸的小部件都将分配到它们所需的最大尺寸空间。 (Widgets do not have to have a maximum size in which case the stretch factor is their determining factor.) (小部件不必具有最大尺寸,在这种情况下,拉伸因子是它们的决定因素。)

And sizeHint() is the recommended size of a QWidget, and the Layout of the widget parent will take sizeHint() and sizePolicy() into consideration to determine the space of the child widget can hold.sizeHint()是QWidget的推荐大小,widget parent的Layout会考虑sizeHint()sizePolicy()来决定子widget可以容纳的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM