设置条件
设置when 条件可评估多个组件属性的真假,并将用户界面移动到这些条件适用的状态。您可以评估某项属性是真还是假,大于还是等于其他属性,等等。还可以使用 AND 或 OR 等运算符来评估多个组件的真假。
确定何时应用状态:
- 转到View >Views >States 。

- 在When Condition 中,选择
。 - 在Binding Editor 中,为状态指定when 属性。
- 将属性值设置为布尔表达式,当要应用状态时,布尔表达式的值为
true。
when 条件从左到右按代码中出现的顺序进行评估。因此,如果两个不同状态的两个条件的评估结果都是true ,则会应用第一个状态。
创建条件绑定
在Binding Editor 中,选择要创建表达式的组件和属性。例如,要在按下按钮时改变状态,可以选择按钮组件及其 pressed 属性。

在Binding Editor 中编写表达式时,代码自动完成功能会列出表达式中可使用的组件及其属性。
逻辑运算符摘要
您可以在表达式中使用以下逻辑运算符,在一个表达式中组合多个条件:
| 运算符 | 意义 | true 如果 |
|---|---|---|
| ! | 不 | 不满足条件。 |
| && | AND | 两个条件都满足。 |
| || | 或 | 满足任一条件。 |
| < | 小于 | 左操作数小于右操作数。 |
| > | 大于 | 左操作数大于右操作数。 |
| >= | 大于或等于 | 左操作数大于或等于右操作数。 |
| <= | 小于或等于 | 左操作数小于或等于右操作数。 |
| == | 相等 | 操作数相等。 |
| === | 严格相等 | 操作数相等且类型相同。 |
| != | 不相等 | 操作数不相等。 |
| !== | 严格不相等 | 操作数类型相同但不相等,或者类型不同。 |
此外,您还可以在校验前使用算术运算符比较数字。不过,我们建议您尽可能为此创建单独的属性。
当条件示例
要在按下按钮时将状态应用到按钮,请编写:
when: control.pressed
要在按钮未按下时应用状态,请选择NOT 。

要在按钮未被按下、选中或悬停时应用状态,请将条件组合如下:
when: !control.pressed && !control.checked && !control.hovered
要在按钮被按下或选中但未悬停时应用状态,请写入:
when: control.pressed || control.checked && !control.hovered
另请参阅 如何使用状态,Qt Quick UI 设计,以及设计Qt Quick UI。
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.