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

通过对象访问单例

怎么回事?

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

为什么这样做不好?

不能以这种方式访问单例。表达式的值将是未定义的。

示例

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 或属性。或者,如果想访问附加属性,请检查是否有错别字。

import QtQml
import QtQuick as QQ

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

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