In questa pagina

Binding duplicati

Questa categoria di avviso viene indicata da qmllint come " [duplicate-property-binding] ".

Intercettatore duplicato su una proprietà

Cosa è successo?

Una proprietà presenta più intercettatori.

Perché è un problema?

L'impostazione di più intercettatori sulla stessa proprietà non è supportata dal motore QML.

Esempio

Proviamo a utilizzare due volte l'intercettatore ` Behavior ` sulla stessa proprietà:

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

Per risolvere questo avviso, rimuovere tutti gli " Behavior" tranne uno:

import QtQuick

Rectangle {
    Behavior on width {
        NumberAnimation { duration: 2000 }
    }
}

Vedi anche Tipi di modificatori di proprietà.

Fonte del valore duplicata sulla proprietà

Cosa è successo?

Una proprietà presenta più fonti di valore.

Perché è un problema?

Le fonti di valore mostreranno un comportamento inaspettato quando combinate. Si veda l'esempio qui sotto.

Esempio

Utilizziamo due volte " NumberAnimation " come fonte di valore per la stessa proprietà:

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

Se si controlla l'output di quel programma, si noterà che i due NumberAnimation si intrecciano tra loro, il che probabilmente non è l'effetto desiderato. Per risolvere questo avviso, rimuovere tutti gli NumberAnimation tranne uno:

import QtQuick

Rectangle {
    NumberAnimation on x { to: 50; duration: 1000 }
}

Impossibile combinare la fonte di valore e il binding

Cosa è successo?

Una proprietà presenta una fonte di valore e un binding sulla stessa proprietà.

Perché è un problema?

Il binding aggiornerà il valore della proprietà prima che la fonte di valore inizi ad aggiornarla. Ciò può causare comportamenti imprevisti e rende il codice più difficile da leggere.

Esempio

Utilizziamo NumberAnimation come fonte di valore per la stessa proprietà:

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

Se si controlla l'output di quel programma, si noterà che NumberAnimation passerà da 55 a 50 in modo animato, cosa che risulterebbe più leggibile con il seguente codice:

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.