本页内容

附加属性的复用

qmllint 将此警告类别拼写为“[attached-property-reuse] ”。

在父作用域中已初始化的关联类型被重复使用

发生了什么?

您多次初始化了一个会传播的关联类型。

注意:这 主要发生在继承自 `QQuickAttachedPropertyPropagator` 的附加类型上。

为什么这不好?

传播型附加对象在每次实例化时都会消耗内存,但实际上只需初始化一次。

示例

import QtQuick
import QtQuick.Templates as T
import QtQuick.Controls.Material // contains the Material attached type

T.ToolBar {
    id: control

    // first instantiation of Material's attached property
    property color c: Material.toolBarColor

    background: Rectangle {
         // second instantiation of Material's attached property, wrong!
        color: Material.toolBarColor
    }
}

要解决此警告,请从父类查询该附着类型:

import QtQuick
import QtQuick.Templates as T
import QtQuick.Controls.Material // contains the Material attached type

T.ToolBar {
    id: control

    // first instantiation of Material's attached property
    property color c: Material.toolBarColor

    background: Rectangle {
        // use control's attached property, correct!
        color: control.Material.toolBarColor
    }
}

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