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