本页内容

属性覆盖

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

成员未覆盖任何内容

发生了什么?

您将一个属性标记为override ,但并未覆盖任何内容。当前组件的基类中不存在被标记为virtual 的同名属性。

为什么这样不好?

当 QML 引擎遇到此类属性时,会抛出运行时错误。

示例

import QtQuick

Item {
    component Base: Item { virtual property int myProperty; }
    Base {
        override property int myP: 42
    }
}

要解决此警告,您可以:

  • 移除override 标记。
  • 仔细检查该属性名称是否与当前组件基类中virtual 或override 属性的名称一致。
import QtQuick

Item {
    component Base: Item { virtual property int myProperty; }
    Base {
        property int myP: 42 // solution 1: remove override
        override property int myProperty: 42 // solution 2: fix spelling
    }
}

成员遮蔽了 final 成员

发生了什么?

您遮蔽了一个被标记为 final 的属性。

为什么这样不好?

属性被标记为 final 是为了防止重写和遮蔽。当 QML 引擎遇到被遮蔽的属性时,会抛出运行时错误。

示例

import QtQuick

Item {
    component Base: Item { final property int myFinal; }
    Base {
        property int myFinal: 42
    }
}

要解决此警告,你可以:

  • 重命名遮蔽或被遮蔽的属性。
  • 将final 标记替换为virtual 或override ,并在重写属性中添加缺失的override 。
import QtQuick

Item {
    // solution 1: rename the property
    component Base: Item { final property int myFinal; }
    Base {
        property int myFinal2: 42
    }

    // solution 2: replace the final marker and add override
    component Base2: Item { virtual property int myFinal; }
    Base2 {
        override property int myFinal: 42
    }
}

成员覆盖了 final 成员

发生了什么?

您重写了一个被标记为 final 的属性。

这有什么问题吗?

标记为final 的属性会阻止重写和遮蔽。当QML引擎遇到重写属性时,会抛出运行时错误。

示例

import QtQuick

Item {
    component Base: Item { final property int myFinal; }
    Base {
        override property int myFinal: 42
    }
}

要解决此警告,你可以:

  • 重命名遮蔽或被遮蔽的属性,并移除override 标记。
  • 将final 标记替换为被覆盖属性中的virtual 。
import QtQuick

Item {
    // solution 1: remove override and rename
    component Base: Item { final property int myFinal; }
    Base {
        property int myFinal2: 42
    }

    // solution 2: replace final marker with virtual
    component Base2: Item { virtual property int myFinal; }
    Base2 {
        override property int myFinal: 42
    }
}

成员遮蔽成员

发生了什么?

您在未使用 `final ` 或 `override` 的情况下,对 `virtual ` 或 `override ` 属性进行了遮蔽。

这有什么问题吗?

遮蔽virtual 属性的属性需要override 或final 标记。QML 引擎在遇到此类属性时会抛出运行时警告。

示例

import QtQuick

Item {
    component Base: Item { virtual property int myProperty; }
    Base {
        property int myProperty: 42
    }
}

要消除此警告,请将遮蔽属性设为override 或final 。

import QtQuick

Item {
    component Base: Item { virtual property int myProperty; }
    Base {
        override property int myProperty: 42 // could also be final
    }
}

成员重写了非虚成员

发生了什么?

您重写了一个未标记为virtual 或override 的属性。

这有什么问题?

可以被重写的属性必须通过virtual 或override 进行声明。当 QML 引擎遇到被重写的属性缺少virtual 或override 标记时,会抛出运行时错误。

示例

import QtQuick

Item {
    component Base: Item { property int myProperty; }
    Base {
        override property int myProperty: 42
    }
}

要解决此警告,请重命名被重写的属性,或将该属性标记为virtual 或override 。

import QtQuick

Item {
    component Base: Item { virtual property int myProperty; }
    Base {
        override property int myProperty: 42
    }
}

属性在基类型中已存在

发生了什么?

您在未使用 asvirtual 、override 或final 的情况下遮蔽了一个属性。

为什么这样做不好?

可以被重写的属性必须通过virtual 、override 或 进行声明。当 QML 引擎在运行时遇到被遮蔽的属性时,会发出警告。

示例

import QtQuick

Item {
    component Base: Item { property int myProperty; }
    Base {
        property int myProperty: 42
    }
}

要解决此警告,请重命名遮蔽或被遮蔽的属性,或添加缺失的virtual 、override 或final 标记。

import QtQuick

Item {
    // solution 1: rename one of the properties
    component Base: Item { property int myProperty; }
    Base {
        property int myProperty2: 42
    }

    // solution 2: add missing markers
    component Base2: Item { virtual property int myProperty; }
    Base2 {
        final property int myProperty: 42 // could also be override
    }
}

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