Qt Quick Controls的尺寸策略

Size Policy for Qt Quick Controls

Qt Quick Controls的尺寸策略

April 05, 2024 by Jan Arve Sæther | Comments

2024年4月5日 Jan Arve Sæther |评论

When using Qt Quick Layouts, it is necessary for the user to specify the attached properties Layout.fillWidth or Layout.fillHeight to stretch a child component. This process can become cumbersome as more components require it. Inline components may serve as an alternative, but they may not be as effective when dealing with different component types. To tackle these challenges, the size policy feature has been introduced in Qt Quick Controls in version 6.7 (under tech preview), mirroring similar functionality found in widgets.

使用Qt Quick布局时,用户需要指定附加特性Layout.fillWidth或Layout.fillHeight来拉伸子构件。随着越来越多的组件需要,这个过程可能会变得繁琐。内联组件可以作为一种替代方案,但在处理不同的组件类型时,它们可能没有那么有效。为了应对这些挑战,在6.7版的Qt Quick Controls中引入了大小策略功能(在技术预览下),反映了widget中的类似功能。

What is the Size policy? 

尺寸策略是什么?

The size policy describes the horizontal and vertical resizing policy the item prefers when laid out within the layout. This affects the item size in terms of grow, shrink, or expand. Qt Widgets already supports this feature, which allows each widget type to communicate with the layouts on how they should be resized. This results in the application developer seldom having to specify the resizing behavior of a widget.

大小策略描述项目在布局中布局时所喜欢的水平和垂直调整大小策略。这会影响项目在增长、收缩或扩展方面的大小。Qt Widget已经支持这一功能,它允许每个Widget类型与布局就如何调整它们的大小进行通信。这导致应用程序开发人员很少需要指定Widget的大小调整行为。

For instance, QLineEdit and QSlider have a default horizontal size policy of QSizePolicy::Expanding, so they will be configured to grow by default, without any extra configuration from the application developer.

例如,QLineEdit和QSlider的默认水平大小策略为QSizePolicy::Expanding,因此它们将被配置为默认增长,而无需应用程序开发人员进行任何额外配置。

Qt Quick Layouts specifies size policies in a bit more primitive way: All items in a Qt Quick Layout are by default fixed size, and the only way to specify if a layout should resize an Item is by explicitly setting  - Layout.fillWidth: true or Layout.fillHeight: true as the attached properties on the item. Note that this allows the item to both shrink and grow beyond its preferred size. 

Qt Quick布局以更原始的方式指定大小策略:默认情况下,Qt Quick版面中的所有项目都是固定大小的,指定版面是否应调整项目大小的唯一方法是显式设置-Layout.fillWidth:true或Layout.fillHeight:true作为项目的附加属性。请注意,这允许项目收缩和增长超过其首选尺寸。

This can lead to some repetitive boilerplate code for common UI components such as TextFields or Sliders where you very often want them to stretch horizontally, but not vertically, so you often end up setting Layout.fillWidth:true each time you declare those items.

这可能会导致常见UI组件(如TextFields或Sliders)的一些重复样板代码,在这些组件中,通常希望它们水平拉伸,但不希望垂直拉伸,因此每次声明这些项时,通常都会设置Layout.fillWidth:true。

Extending Qt Quick Layouts with Size Policies 

用大小策略扩展Qt Quick布局

To avoid this boilerplate code, we thought it might be a good idea to introduce the same QWidget size policy mechanism to Quick Items, but due to compatibility reasons, the application developer has to enable it explicitly. By enabling this feature, the fixed size assumption by Qt Quick Layouts will be overridden with type specific size policies - so e.g. Button will have a size policy which is preferred horizontally and fixed vertically. 

为了避免这种样板代码,我们认为将相同的QWidget大小策略机制引入Quick Items可能是个好主意,但由于兼容性原因,应用程序开发人员必须明确启用它。通过启用此功能,Qt Quick布局的固定大小假设将被特定类型的大小策略覆盖,因此按钮将具有首选的水平和垂直大小策略。

For now, this feature can be enabled in Qt Quick by setting the global application attribute Qt::AA_QtQuickUseDefaultSizePolicy,

目前,可以通过设置全局应用程序属性Qt::AA_QtQuickUseDefaultSizePolicy在Qt Quick中启用此功能,


QGuiApplication::setAttribute(Qt::AA_QtQuickUseDefaultSizePolicy);

How will it be useful for the user? 

它将如何对用户有用?

The users can now avoid setting the attached properties Layout.fillWidth / Layout.fillHeight each time they add a quick item in the layout. Qt Widgets users can migrate to Qt Quick with the expectation that the default layout policy is set for the items within the layout.

用户现在可以避免每次在布局中添加快速项目时设置附加属性Layout.fillWidth/Layout.fillHeight。Qt Widget用户可以迁移到Qt Quick,期望为布局中的项目设置默认布局策略。

In the example provided below, the Layout attached property can be removed if the default size policy is enabled in the application.

在下面提供的示例中,如果应用程序中启用了默认大小策略,则可以删除附加布局的属性。


RowLayout {
    Label {
        text: "Name"
    }
    TextField {
        placeholderText: "Surname, firstname"
        Layout.fillWidth: true   // <-- Can be removed if default size policy is enabled
    }
}

Current Pitfalls:

当前缺陷:

  • The solution of using a global switch has a drawback when external components are used in the application (such as components from a third-party library unaware of this feature). This is under discussion, and it will be fixed.

  • 当应用程序中使用外部组件(例如来自不知道此功能的第三方库的组件)时,使用全局开关的解决方案有一个缺点。这一问题正在讨论中,并将得到解决。

  • The size policy of quick items is currently internal and cannot be explicitly set, but it is still possible to adjust with existing layout attached properties (using Layout.fillWidth and Layout.fillHeight

  • quick项目的大小策略当前是内部的,无法显式设置,但仍可以使用现有布局附加属性进行调整(使用layout.fillWidth和layout.fillHeight)

  • The quick item in 6.7.0 does not support the Expanding or Minimum policy, but it shall be supported in the future version. 

  • 6.7.0中的quick项不支持扩展或最小策略,但在未来版本中应支持它。

 Extend for the future: 

面向未来扩展:

  • The size policy property can be exposed, and the user can set or override default policies for the quick items placed within the layout.

  • 大小策略属性可以公开,用户可以设置或覆盖布局中quick项目的默认策略。

Since this feature is under tech preview, we would like users to experiment this feature and provide their feedback. 

由于此功能正在技术预览中,我们希望用户试用此功能并提供反馈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值