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