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