本页内容

别名循环

qmllint 将此警告类别拼写为[alias-cycle] 。

别名属性属于别名循环的一部分

发生了什么?

一个属性别名解析为其自身,或者解析为另一个同样解析为自身的别名。

通常,属性别名应直接引用另一个属性,或通过另一个别名属性间接引用。

如果属性别名直接或间接地引用了自身,则会形成别名循环。该警告表明当前的别名属性位于别名循环内或引用了别名循环,请参见示例。

这有什么问题?

包含别名循环的组件实例在运行时不会被创建:它们将被视为 null。

示例

import QtQuick

Item {
    id: someId
    property alias myself: someId.myself // not ok: referring to itself

    property alias cycle: someId.cycle2 // not ok: indirectly referring to itself
    property alias cycle2: someId.cycle

    property alias indirect: someId.cycle // not ok: referring to alias indirectly referring to itself
}

要解决此警告,请打破别名循环:

import QtQuick

Item {
    id: someId
    Item {
        id: anotherId
        property string myself
        property int cycle
    }
    property alias myself: anotherId.myself // ok: referring to a property

    property alias cycle: someId.cycle2 // ok: does not refer to itself anymore
    property alias cycle2: anotherId.cycle // ok: not a cycle anymore

    property alias indirect: someId.cycle // ok: cycle does not form an alias cycle anymore
}

© 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.