本页内容

语法

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 不是有效的类型名,因为它是一个属性名。要解决此警告,请使用有效的 Type 作为组件的基类型:

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{}
    ]
}

pragma 参数未知

发生了什么?

您为pragma 指定了无效的参数。

这有什么问题?

该 pragma 将无法生效。

示例

pragma ComponentBehavior: Buond
import QtQuick

Item {
}

您可以通过删除该 pragma 或修正潜在的拼写错误来解决此警告:

pragma ComponentBehavior: Bound
import QtQuick

Item {
}

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