本页内容

缺少属性

qmllint 将此警告类别拼写为[missing-property] 。

无法将值赋给不存在的默认属性

发生了什么?

您将一个对象赋值给了不存在的默认属性。

为什么这样不好?

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

示例

import QtQuick

Item {
    component MyType: QtObject { property Item myItem; }

    MyType {
        Item {}
    }
}

要解决此警告,请指定要绑定的属性;或者,如果您是该类型的作者,请将某个属性标记为默认属性:

import QtQuick

Item {
    component MyType: QtObject { property Item myItem; }

    MyType {
        myItem: Item {}
    }

    component AlternativeMyType: QtObject { default property Item myItem; }

    AlternativeMyType {
        Item {} // bound to myItem via default property
    }
}

属性不存在

发生了什么?

您将一个表达式赋值给了不存在的属性。

为什么这样不好?

QML 引擎无法在运行时将此表达式赋值给该属性。

示例

import QtQuick

Item {
    property int myInt
    myItn: 42
}

要解决此警告,请删除该绑定或更正可能的拼写错误:

import QtQuick

Item {
    property int myInt
    myInt: 42
}

类型中未找到成员

发生了什么?

您在字段成员表达式中访问了一个成员,但 QML 工具无法找到该成员。字段成员表达式采用以下形式:someId.someProperty 。

该成员可能根本不存在,例如您输入时出现了拼写错误。或者它可能在运行时存在(因为它存在于一个始终存在的特定子类型上),但在静态声明的类型上不存在。

这有什么问题?

QML 工具无法找到该成员,这意味着“转到定义”和“自动完成”等功能将无法正常工作。 在运行时,如果该成员确实不存在,可能会引发错误或导致结果不正确。如果该成员在运行时实际上始终存在,则不会报错,但在这种情况下,由于错过了优化机会,仍可能产生性能开销。

示例

属性实际上并不存在

import QtQuick

Item {
    id: self
    property int myInt
    property int myInt2: 1 + self.myItn
}

要解决此警告,请删除该绑定或更正可能的拼写错误:

import QtQuick

Item {
    id: self
    property int myInt
    property int myInt2: 1 + self.myInt
}

属性存在,但类型不够精确

import QtQuick
import QtQuick.Controls.Basic

Item {
  component Message : Item {
    required property string sender
    required property string text
  }
  ListView {
    id: messageView
    delegate: Message {}
  }

  Button {
    text: "Reply to %1".arg(messageView.currentItem.sender) // not ok
  }
}

要解决此警告,请将读取到的对象强制转换为更具体的类型:

import QtQuick
import QtQuick.Controls.Basic

Item {
  component Message : Item {
    required property string sender
    required property string text
  }
  ListView {
    id: messageView
    delegate: Message {}
  }

  Button {
    text: "Reply to %1".arg((messageView.currentItem as Message).sender) // now ok
  }
}

在此示例中,messageView 的currentItem 属性的声明类型为Item ,而该类型不包含sender 属性:sender 仅在Message 中定义。不过,我们知道currentItem 始终包含Message ,因此可以使用类型断言将 currentItem 强制转换为Message 。

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