本页内容

QObjectRegistryRef Class

该类用于从 C++ 代码中访问已注册为 `ObjectRegistry ` QML 类型的对象。更多内容...

头文件: #include <QObjectRegistryRef>
CMake: find_package(Qt6 REQUIRED COMPONENTS QmlDesignSupport)
target_link_libraries(mytarget PRIVATE Qt6::QmlDesignSupport)
qmake: QT += qmldesignsupport
自: Qt 6.12
在 QML 中:MultiObjectRegistryRef 以及ObjectRegistryRef
继承自: QAbstractObjectRegistryRef

属性

公共函数

QObjectRegistryRef(QObject *parent = nullptr)
QObjectRegistryRef(QQmlEngine *engine, QObject *parent = nullptr)
QObjectRegistryRef(QQmlEngine *engine, const QString &key, QObject *parent = nullptr)
QObject *object() const

信号

void objectChanged(QObject *object)

重新实现的受保护函数

virtual bool event(QEvent *event) override

详细说明

使用示例:

// Register object in a QML file
MouseArea {
    ObjectRegistry.key: "MyMouseArea"
}

// In C++ code, refer to the registered object, using the QML engine you loaded the QML with
auto ref = new QObjectRegistryRef(engine, "MyMouseArea");
const auto object = ref->object();
connect(object, SIGNAL(clicked(QQuickMouseEvent*)),
        this, SLOT(myClickHandler()));

另请参阅 ObjectRegistry 和QAbstractObjectRegistryRef::key 。

属性文档

[read-only] object : QObject*

该属性存储了由该实例引用的对象。

如果使用同一个键注册了多个对象,则该实例引用的对象是其中一个(具体哪个未指定)。在这种情况下,请改用QMultiObjectRegistryRef 。

访问函数:

QObject *object() const

通知器信号:

void objectChanged(QObject *object)

另请参阅 ObjectRegistry 和QAbstractObjectRegistryRef::key 。

成员函数文档

[explicit] QObjectRegistryRef::QObjectRegistryRef(QObject *parent = nullptr)

使用parent 创建QObjectRegistryRef实例。

注意: 仅当设置键时能自动解析出正确的 QML 引擎时,才应使用 此构造函数。从 C++ 创建本类的实例时,通常不会发生这种情况。建议使用那些将 QML 引擎指针作为参数的构造函数之一。

另请参阅 qmlEngine() 和QAbstractObjectRegistryRef::key 。

[explicit] QObjectRegistryRef::QObjectRegistryRef(QQmlEngine *engine, QObject *parent = nullptr)

使用 QML 中的 `engine ` 和 `parent` 构造 `QObjectRegistryRef` 实例。所有注册操作的作用域均限定在指定的 QML 引擎内。

另请参阅 QAbstractObjectRegistryRef::key 。

[explicit] QObjectRegistryRef::QObjectRegistryRef(QQmlEngine *engine, const QString &key, QObject *parent = nullptr)

使用 QML 中的engine 、key 和parent 构建 QObjectRegistryRef 实例。所有注册均限定在指定的 QML 引擎范围内。

另请参阅 QAbstractObjectRegistryRef::key 。

[override virtual protected] bool QObjectRegistryRef::event(QEvent *event)

重写了:QAbstractObjectRegistryRef::event(QEvent *event)。

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