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