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
non-list-property.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-non-list-property.html
6
\ingroup qmllint-warnings-and-errors
7
8
\title Non-list property
9
\brief [non-list-property] Multiple values were assigned to a non-list property.
10
11
\qmllintwarningcategory non-list-property
12
13
\section1 Cannot assign multiple objects to a default non-list property
14
15
\section2 What happened?
16
A \l{Default Properties}{default property} has multiple bindings but the default
17
property type is not a list type and only expects one binding.
18
19
\section2 Why is this bad?
20
All the bindings to the default property, except the last one, will be ignored. This most likely
21
hints that the default property should instead be a list, or that there are too many bindings to
22
the same property.
23
24
\section2 Example
25
26
Let's declare a component \c{MyComponent} that has one default non-list property, and then lets
27
bind three items to that default property:
28
\qml
29
import QtQuick
30
31
Item {
32
component MyComponent: QtObject {
33
default property Item helloWorld
34
}
35
MyComponent {
36
// first item bound to default property:
37
Item { objectName: "first" } // will warn: Cannot assign multiple objects to a default non-list property [non-list-property]
38
// second item bound to default property:
39
Item { objectName: "second" } // not ok: default property was bound already
40
// third item bound to default property:
41
Item { objectName: "third" } // not ok: default property was bound already
42
43
Component.onCompleted: console.log(helloWorld.objectName) // prints "third"
44
}
45
}
46
47
\endqml
48
To fix this warning, replace the default property by a list:
49
\qml
50
import QtQuick
51
52
Item {
53
component MyComponent: QtObject {
54
default property list<Item> helloWorld
55
}
56
MyComponent {
57
// first item bound to default property:
58
Item { objectName: "first" } // ok: binding a first item to the list
59
// second item bound to default property:
60
Item { objectName: "second" } // ok: binding a second item to the list
61
// third item bound to default property:
62
Item { objectName: "third" } // ok: binding a third item to the list
63
}
64
}
65
\endqml
66
To fix this warning, remove all the unwanted bindings in case the default property
67
is not supposed to be a list:
68
\qml
69
import QtQuick
70
71
Item {
72
component MyComponent: QtObject {
73
default property Item helloWorld
74
}
75
MyComponent {
76
Item { objectName: "first" } // ok: just one item bound to default property
77
}
78
MyComponent {
79
Item { objectName: "second" } // ok: just one item bound to default property
80
}
81
MyComponent {
82
Item { objectName: "third" } // ok: just one item bound to default property
83
}
84
}
85
\endqml
86
*/
qtdeclarative
src
qml
doc
src
qmllint
non-list-property.qdoc
Generated on
for Qt by
1.14.0