在本页中

首选非变量属性

此警告类别由 qmllint 拼写[prefer-non-var-properties]

优先选择更具体的类型而不是 var

发生了什么事?

您在 QML 中定义的属性类型是var ,而不是更具体的类型,例如intstring

为什么这样不好?

这会影响代码的可读性,而且 QML 工具无法应用特定的类型优化。

您应避免使用var 作为总是持有相同类型的属性的属性类型。为避免误报,qmllint 只对绑定简单的只读属性发出警告。在其他情况下,qmllint 可能无法区分必须为 var 的属性(因为它们需要持有多种类型)和始终持有相同类型的属性。

示例

import QtQuick

Item {
    readonly property var myP: 42
}

要修复此警告,请将var 替换为特定类型。

import QtQuick

Item {
    readonly property int myP: 42
}

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