重复绑定
此警告类别由 qmllint 拼写[duplicate-property-binding]
。
属性上的重复拦截器
发生了什么事?
一个属性有多个拦截器。
为什么会这样?
QML 引擎不支持在同一属性上设置多个拦截器。
示例
让我们在同一个属性上使用Behavior 作为拦截器两次:
import QtQuick Rectangle { Behavior on width { NumberAnimation { duration: 1000 } } Behavior on width { // not ok: Duplicate interceptor on property "width" [duplicate-property-binding] NumberAnimation { duration: 2000 } } }
要修复此警告,请删除所有拦截器,只保留一个Behavior :
import QtQuick Rectangle { Behavior on width { NumberAnimation { duration: 2000 } } }
另请参阅 属性修改器类型。
属性上的重复值源
发生了什么?
一个属性有多个值源。
为什么会这样?
当值源组合在一起时,会出现意想不到的行为。请看下面的示例。
示例
让我们在同一属性上两次使用NumberAnimation 作为值源:
import QtQuick Rectangle { NumberAnimation on x { to: 50; duration: 1000 } NumberAnimation on x { to: 10; duration: 100 } // not ok: Duplicate value source on property "x" [duplicate-property-binding] onXChanged: console.log(x) }
如果检查该程序的输出,就会发现两个NumberAnimation 会相互交错,这可能不是预期的效果。要修复此警告,请删除除一个NumberAnimation 以外的所有内容:
import QtQuick Rectangle { NumberAnimation on x { to: 50; duration: 1000 } }
无法合并值源和绑定
发生了什么?
一个属性在同一个属性上有一个值源和一个绑定。
为什么会这样?
绑定会在值源开始更新该属性之前更新属性值。这可能会导致意想不到的行为,而且更难读取。
示例
让我们使用NumberAnimation 作为同一属性的值源:
import QtQuick Rectangle { NumberAnimation on x { to: 50; duration: 1000 } // not ok: Cannot combine value source and binding on property "x" [duplicate-property-binding] x: 55 onXChanged: console.log(x) }
如果查看该程序的输出,您会发现NumberAnimation 将从 55 动画到 50,如果使用下面的代码,将更容易阅读:
import QtQuick Rectangle { NumberAnimation on x { from: 55; to: 50; duration: 1000 } // ok: intentions are clearer now! }
© 2025 The Qt Company Ltd. 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.