本页面

无意空块

此警告类别由 qmllint 拼写为[unintentional-empty-block]

无意空块

发生了什么?

一个空块被声明为属性绑定的表达式。

你可能想声明一个空对象字面量。为此,请用括号将字面意义包起来。这样做是为了将字面意义与空代码块区分开来。

为什么这样做不好?

将一个没有任何说明的代码块指定为属性绑定并没有任何作用,而且可能会让读者感到困惑。在评估该绑定时,不会执行任何指令,绑定的结果将是未定义。

示例

import QtQml

QtObject {
    property var v: {} // This is not an empty object literal!
}

要解决这个警告,可以用括号将对象字面意义包起来,或者完全删除绑定:

import QtQml

QtObject {
    property var v: ({}) // This is an empty object literal
}

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