本页内容

类型不兼容

qmllint 将此警告类别拼写为[incompatible-type] 。

无法将值赋给类型不兼容的默认属性

发生了什么?

您将一个对象赋值给了类型不兼容的默认属性。

这有什么问题?

QML 引擎将在运行时无法将该对象赋值给该属性。

示例

import QtQuick

Item {
    component MyType: QtObject {
        default property list<Item> myDefaultProperty
    }

    MyType {
        QtObject {} // note: QtObject does not inherit from Item
    }
}

要解决此警告,请将兼容的类型绑定到该属性上;或者,如果您是该默认属性的作者,请在定义中修改类型:

import QtQuick

Item {
    component MyType: QtObject {
        default property list<Item> myDefaultProperty
    }

    MyType {
        Item {}
    }

    component AlternativeMyType: QtObject {
        default property list<QtObject> myDefaultProperty
    }

    AlternativeMyType {
        QtObject {} // is ok for AlternativeMyType
    }
}

属性的绑定类型错误

发生了什么?

您使用了无效的属性修饰符类型。

这有什么问题?

QML 引擎将无法在运行时使用该属性修饰符类型。

示例

import QtQuick

Item {
    property int xxx
    Item on xxx { ... }
}

要解决此警告,请删除on ,或使用有效的属性修饰符类型:

import QtQuick

Item {
    property int xxx
    Item { ... }

    // Alternative: use a valid property modifier type
    NumberAnimation on xxx { ... }
}

从字符串进行构造已被废弃;请改用结构化值类型构造

发生了什么?

您使用字符串创建了一个QML_STRUCTURED_VALUE 。

为什么这样不好?

此方法已弃用,且容易出现拼写错误。

示例

import QtQuick

Item {
    property point p: "5, 6"
}

要解决此警告,请按照QML_STRUCTURED_VALUE 的说明为结构化值类型赋值,而不是将字符串绑定到属性上:

import QtQuick

Item {
    property point p: ({ x: 5, y: 6 })
}

未标注返回类型的函数会返回

发生了什么?

您从一个未标注返回类型的函数中返回了一个值。

为什么这样不好?

您已通过注解指定该函数不返回任何内容,因此该函数不应返回任何值。Qt Qml 工具将无法处理该方法,且在未来的 Qt 版本中,Qt Qml 引擎将忽略该返回值。

示例

import QtQuick

Item {
    function f(x: int) {
        ...
        return x
    }
}

要解决此警告,请将函数签名调整为新的返回类型,或移除返回值:

import QtQuick

Item {
    function f(x: int): int {
        ...
        return x
    }
    function alternativeF(x: int) {
        ...
        return
    }
}

无法将绑定/对象/字面量进行赋值

发生了什么?

您将一个对象、字面量或表达式绑定到了类型不兼容的属性上。

为什么这样不好?

QML 引擎将无法在运行时将该对象、字面量或表达式进行赋值。

示例

import QtQuick

Item {
    property date xxx: 42
}

要解决此警告,请绑定类型兼容的对象、值或表达式:

import QtQuick

Item {
    property date xxx: new Date()
}

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