本页内容

只读属性

qmllint 将此警告类别拼写为[read-only-property] 。

无法将值赋给只读属性

发生了什么?

对只读属性进行了写入操作。

为什么这样做不好?

当 QML 引擎检测到对只读属性的写入操作时,会抛出 TypeError 异常。

示例

import QtQuick

Item {
    id: root
    readonly property int someNumber: 10

    Component.onCompleted: {
        someNumber = 20  // not ok: TypeError: Cannot assign to read-only property
    }
}

要解决此警告,请移除对只读属性的写入操作,改写入另一个非只读属性,或者如果您是该属性定义的作者,请移除 readonly 修饰符。

另请参阅 “只读属性”。

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