本页内容

重复的绑定

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!
}

© 2026 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.