语法

此警告类别由 qmllint 拼写[syntax]

不支持嵌套内联组件

发生了什么事?

您在另一个内联组件中定义了一个内联组件。

为什么这样做不好?

QML 语言不允许嵌套内联组件。应始终在 QML 文件的根项中定义内联组件。

示例

import QtQuick

Item {
    component Correct: Item {
        component Evil: Item { ... }
        ...
    }
}

要解决这个警告,把所有组件移到 QML 文件的根项。

import QtQuick

Item {
    component NotEvilAnymore: Item { ... }
    component Correct: Item {
        ...
    }
}

内联组件声明后必须有类型名

发生了什么事?

你定义的内联组件的基本类型无效。

为什么会这样?

内联组件需要一个基类型来继承。

示例

import QtQuick

Item {
    property Item someProperty
    component InlineComponent: someProperty {}
}

在本例中,someProperty 不是一个有效的类型名称,因为它是一个属性名称。要修复此警告,请使用有效的类型作为组件的基础类型:

import QtQuick

Item {
    property Item someProperty
    component InlineComponent: Item { ... }
}

无效别名表达式:需要初始化器

发生了什么事?

你定义了一个属性别名,但没有定义被别名的属性。

为什么会这样?

别名属性总是需要在定义中包含其别名属性或 id。

示例

import QtQuick

Item {
    id: root
    property int someProperty
    property alias aliasProperty
}

要修复此警告,请用正常属性替换别名,或添加缺失的别名属性:

import QtQuick

Item {
    id: root
    property int someProperty
    property alias withAliasedProperty: root.someProperty
}

无效别名表达式:只有 id 和字段成员表达式可以别名

发生了什么事?

你定义的属性别名所别名的表达式不是 ID 或字段成员表达式。

字段成员表达式是形式为someId.someProperty 的表达式。

为什么这样做不好?

别名属性总是需要在定义中包含其别名属性,并且不能绑定到 ID 和字段成员表达式以外的其他表达式。

示例

import QtQuick

Item {
    property int p
    property alias someProperty: p + 1
}

要修复此警告,请将别名替换为普通属性,或将其绑定到 ID 或字段成员表达式:

import QtQuick

Item {
    id: root
    property int p
    property int someProperty: p + 1
    property alias alternative: root.p
}

Id 后面必须跟一个标识符

发生了什么事?

你定义了一个没有值的id

为什么会这样?

QML 语言不允许空 id。

示例

import QtQuick

Item {
    id:;
}

要修复此警告,请将 id 绑定到一个有效名称:

import QtQuick

Item {
    id: root;
}

解析 id 失败

发生了什么?

你把id绑定到了一个表达式,而不是一个名称。

为什么会这样?

QML 语言只允许名称绑定 id,不能使用更复杂的表达式。

示例

import QtQuick

Item {
    property int a
    property int b
    function f() {
        if (true)
            return a
        return b
    }

    id: f()
}

要修复此警告,可将 id 绑定到一个有效的名称,或声明一个属性并设置绑定:

import QtQuick

Item {
    property int a
    property int b
    function f() {
        if (true)
            return a
        return b
    }

    id: someItem // it would be confusing to call it `f` like the function
    property int alternative: f()
}

声明一个不是 QML 对象的对象作为列表成员

发生了什么事?

你在对象列表中添加了一个非对象的表达式。

为什么这样做不好?

QML 语言只允许对象在对象列表中。

示例

import QtQuick

Item {
    property int hello
    property list<Item> myList: [
        Item {}, hello{}
    ]
}

要修复此警告,请使用有效的对象类型,或从列表中删除该项目:

import QtQuick

Item {
    component Hello: Item {}
    property list<Item> myList: [
        Item {}, Hello{}
    ]
}

内联组件中声明的枚举将被忽略

发生了什么?

您在内联组件中定义了一个枚举

为什么会这样?

QML 语言只允许在 QML 文件的根项中定义枚举。在内联组件内声明的枚举是不可用的,即使在内联组件内也是如此。这同样适用于在非根 QML 对象中声明的枚举。

示例

import QtQuick

Item {
    component MyInlineComponent: Item {
        enum MyEnum { Hello, World }
    }
}

要解决这个警告,把枚举声明移到 QML 文件的根元素中:

import QtQuick

Item {
    enum MyEnum { Hello, World }
    component MyInlineComponent: Item {
    }
}

未知 pragma 参数

发生了什么事?

您为pragma 指定了一个无效参数。

为什么会这样?

该 pragma 将不起作用。

示例

pragma ComponentBehavior: Buond
import QtQuick

Item {
}

您可以通过删除该 pragma 或修正潜在的错字来修复此警告:

pragma ComponentBehavior: Bound
import QtQuick

Item {
}

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