本页内容

ObjectRegistry QML Type

此类型用于注册对象,以便日后根据键字符串进行访问。更多内容...

Import Statement: import QtQml.DesignSupport
Since: Qt 6.12

属性

详细说明

对象注册表为对象提供了贯穿整个 QML 引擎的引用机制。您需使用字符串键注册每个对象,之后可通过以下引用类型之一检索该对象:ObjectRegistryRef 和MultiObjectRegistryRef 。此外,还提供了用于访问已注册对象的 C++ 类:QObjectRegistryRef 和QMultiObjectRegistryRef 。

同一个键可以用于注册多个对象。典型的用例是注册列表或重复器委托。

当对象销毁或 ObjectRegistry 实例销毁时,已注册的对象会自动从注册表中移除。更改目标对象或键值会将旧的组合从注册表中移除。

请勿将同一对象与同一键的组合注册多次。每个组合仅存储一次注册信息。

注册可通过注册对象上的附加属性完成,也可使用单独的 ObjectRegistry 对象并指定目标和键:

// Registering with attached property
Item {
    id: itemToRegister
    ObjectRegistry.key: "MyItem"
}

// Registering with separate ObjectRegistry object
Item {
    id: itemToRegister
    ObjectRegistry {
        target: itemToRegister
        key: "MyItem"
    }
}

另请参阅 ObjectRegistryRef 、MultiObjectRegistryRef 、QObjectRegistryRef 以及QMultiObjectRegistryRef 。

属性文档

key : string

该属性指定了已注册对象的键。

target : QtObject

此属性指定要注册的目标对象。

如果目标对象被销毁,则该属性将重置为 null。

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