本页内容

非列表属性

qmllint 将此警告类别拼写为[non-list-property] 。

无法将多个对象赋值给默认的非列表属性

发生了什么?

一个默认属性具有多个绑定,但该默认属性的类型并非列表类型,且仅预期一个绑定。

为什么这有问题?

除最后一个绑定外,所有绑定到该默认属性的绑定都将被忽略。这很可能表明该默认属性应改为列表类型,或者同一属性上的绑定过多。

示例

让我们声明一个名为MyComponent 的组件,该组件有一个默认的非列表属性,然后将三个项绑定到该默认属性上:

import QtQuick

Item {
    component MyComponent: QtObject {
        default property Item helloWorld
    }
    MyComponent {
        // first item bound to default property:
        Item { objectName: "first" } // will warn: Cannot assign multiple objects to a default non-list property [non-list-property]
        // second item bound to default property:
        Item { objectName: "second" } // not ok: default property was bound already
        // third item bound to default property:
        Item { objectName: "third" } // not ok: default property was bound already

        Component.onCompleted: console.log(helloWorld.objectName) // prints "third"
    }
}

要解决此警告,请将默认属性替换为列表:

import QtQuick

Item {
    component MyComponent: QtObject {
        default property list<Item> helloWorld
    }
    MyComponent {
        // first item bound to default property:
        Item { objectName: "first" } // ok: binding a first item to the list
        // second item bound to default property:
        Item { objectName: "second" } // ok: binding a second item to the list
        // third item bound to default property:
        Item { objectName: "third" } // ok: binding a third item to the list
    }
}

若默认属性本不应为列表,则需移除所有不需要的绑定以消除此警告:

import QtQuick

Item {
    component MyComponent: QtObject {
        default property Item helloWorld
    }
    MyComponent {
        Item { objectName: "first" } // ok: just one item bound to default property
    }
    MyComponent {
        Item { objectName: "second" } // ok: just one item bound to default property
    }
    MyComponent {
        Item { objectName: "third" } // ok: just one item bound to default property
    }
}

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.