别名周期
此警告类别由 qmllint 拼写[alias-cycle]
。
别名属性是别名循环的一部分
发生了什么?
一个属性别名解析到自身或另一个别名解析到自身。
通常,一个属性别名应直接引用另一个属性,或通过另一个别名属性间接引用另一个属性。
如果一个属性别名直接或间接引用自身,就会形成一个别名循环。警告表示当前别名属性位于别名循环内部或引用了别名循环,请参阅示例。
为什么会出现这种情况?
运行时将不会创建具有别名循环的组件实例:它们将被置为空。
示例
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 }
© 2025 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.