Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
doc_src_objectregistry.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4//! [0]
5// Register object in a QML file
7 ObjectRegistry.key: "MyMouseArea"
8}
9//! [0]
10
11//! [1]
12// In another QML file, refer to the registered object
13ObjectRegistryRef {
14 id: mouseAreaRef
15 key: "MyMouseArea"
16
17 Connections {
18 target: mouseAreaRef.object
19 function onClicked(mouse) {
20 console.log("Mouse clicked")
21 }
22 }
23}
24//! [1]
25
26//! [2]
27// In C++ code, refer to the registered object, using the QML engine you loaded the QML with
28auto ref = new QObjectRegistryRef(engine, "MyMouseArea");
29const auto object = ref->object();
30connect(object, SIGNAL(clicked(QQuickMouseEvent*)),
31 this, SLOT(myClickHandler()));
32//! [2]
33
34//! [3]
35// In a QML file, register something that gets created multiple times (e.g. a repeater delegate)
37 model: 4
38 Rectangle {
39 id: qmlRepeaterDelegate
40 required property int index
41 x: 50 * index
42 width: 40
43 height: 40
44 MouseArea {
45 property int repeaterIndex: qmlRepeaterDelegate.index
46 anchors.fill: parent
47 ObjectRegistry.key: "RepeatedMouseArea"
48 }
49 }
50}
51//! [3]
52
53//! [4]
54// In another QML file, refer to the registered objects
55MultiObjectRegistryRef {
56 id: repeaterRef
57 key: "RepeatedMouseArea"
58
59 onObjectAdded: (object)=> {
60 // Connect each object's clicked signal as they are added
61 object.clicked.connect(function (mouse) { clickedHandler(object); })
62 }
63
64 function clickedHandler(object) {
65 console.log("Index clicked: " + object.repeaterIndex)
66 }
67}
68//! [4]
69
70//! [5]
71// In C++ code, refer to the registered objects, using the QML engine you loaded the QML with
72auto ref = new QMultiObjectRegistryRef(engine, "RepeatedMouseArea");
73const auto repeaterObjects = ref->objects();
74for (auto obj : repeaterObjects) {
75 connect(obj, SIGNAL(clicked(QQuickMouseEvent*)),
76 this, SLOT(handleRepeaterObjectClicked()));
77}
78
79void MyClass::handleRepeaterObjectClicked()
80{
81 qDebug() << "Clicked rectangle index: " << sender()->property("repeaterIndex").toInt()
82}
83//! [5]
84
85//! [6]
86// Registering with attached property
87Item {
88 id: itemToRegister
89 ObjectRegistry.key: "MyItem"
90}
91
92// Registering with separate ObjectRegistry object
93Item {
94 id: itemToRegister
95 ObjectRegistry {
96 target: itemToRegister
97 key: "MyItem"
98 }
99}
100//! [6]
const auto repeaterObjects
const auto object
connect(manager, &QNetworkAccessManager::finished, this, &MyClass::replyFinished)
QAtomicInt ref
Definition qimage_p.h:43