Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
duplicate-property-binding.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\ingroup qmllint-warnings-and-errors
6
7\title Duplicate bindings
8\brief [duplicate-property-binding] A property was bound multiple times.
9
10\qmllintwarningcategory duplicate-property-binding
11
12\section1 Duplicate interceptor on property
13
14\section2 What happened?
15One property has multiple \l{Property Modifier Types}{interceptors}.
16
17\section2 Why is this bad?
18Setting multiple interceptors on the same property is unsupported by the QML engine.
19
20\section2 Example
21
22Lets use \l{Behavior} as interceptor twice on the same property:
23\qml
24import QtQuick
25
26Rectangle {
27 Behavior on width {
28 NumberAnimation { duration: 1000 }
29 }
30 Behavior on width { // not ok: Duplicate interceptor on property "width" [duplicate-property-binding]
31 NumberAnimation { duration: 2000 }
32 }
33}
34\endqml
35To fix this warning, remove all but one \l{Behavior}:
36\qml
37import QtQuick
38
39Rectangle {
40 Behavior on width {
41 NumberAnimation { duration: 2000 }
42 }
43}
44\endqml
45
46\b {See also} \l {Property Modifier Types}.
47
48\section1 Duplicate value source on property
49
50\section2 What happened?
51One property has multiple \l{Property Value Sources}{value sources}.
52
53\section2 Why is this bad?
54The value sources will show unexpected behavior when combined. See the example below.
55
56\section2 Example
57
58Lets use \l{NumberAnimation} as value source twice on the same property:
59\qml
60import QtQuick
61
62Rectangle {
63 NumberAnimation on x { to: 50; duration: 1000 }
64 NumberAnimation on x { to: 10; duration: 100 } // not ok: Duplicate value source on property "x" [duplicate-property-binding]
65
66 onXChanged: console.log(x)
67}
68\endqml
69
70If you check the output of that program, you will see that the two NumberAnimation will interleave
71each other, which is probably not the effect that was intended.
72To fix this warning, remove all but one \l{NumberAnimation}:
73\qml
74import QtQuick
75
76Rectangle {
77 NumberAnimation on x { to: 50; duration: 1000 }
78}
79\endqml
80
81
82\section1 Cannot combine value source and binding
83
84\section2 What happened?
85One property has a \l{Property Value Sources}{value source} and a binding on the same property.
86
87\section2 Why is this bad?
88The binding will updated the property value before the value source starts updating this property.
89This may lead to unexpected behavior, and is also harder to read.
90
91\section2 Example
92
93Lets use \l{NumberAnimation} as value source on the same property:
94\qml
95import QtQuick
96
97Rectangle {
98 NumberAnimation on x { to: 50; duration: 1000 } // not ok: Cannot combine value source and binding on property "x" [duplicate-property-binding]
99 x: 55
100
101 onXChanged: console.log(x)
102}
103\endqml
104
105If you check the output of that program, you will see that the \l{NumberAnimation} will animate
106from 55 to 50, which would be easier to read with following code:
107\qml
108import QtQuick
109
110Rectangle {
111 NumberAnimation on x { from: 55; to: 50; duration: 1000 } // ok: intentions are clearer now!
112}
113\endqml
114
115*/