本页内容

无法将单例作为对象的属性进行访问

qmllint 将此警告类别拼写为“[access-singleton-via-object] ”。

通过对象访问单例

发生了什么?

您使用了从命名空间访问附加属性的语法来访问单例。

为什么这样不好?

无法通过这种方式访问单例。该表达式将求值为 undefined。

示例

import QtQml
import QtQuick as QQ

QtObject {
    id: root
    // Cannot access singleton as a property of an object. Did you want to access an attached object?
    property var singletonAccess: root.QQ.Application.platform
}

要解决此警告,如果您打算使用该单例,请移除命名空间前面的id 或property。或者,如果您想访问附着属性,请检查是否有拼写错误。

import QtQml
import QtQuick as QQ

QtObject {
    id: root
    property var singletonAccess: QQ.Application.platform
    property bool attachedPropertyAccess: root.QQ.ListView.isCurrentItem
}

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