Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
6
MouseArea
{
7
ObjectRegistry.key:
"MyMouseArea"
8
}
9
//! [0]
10
11
//! [1]
12
// In another QML file, refer to the registered object
13
ObjectRegistryRef {
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
28
auto
ref
=
new
QObjectRegistryRef(engine,
"MyMouseArea"
);
29
const
auto
object
= ref->object();
30
connect
(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)
36
Repeater
{
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
55
MultiObjectRegistryRef {
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
72
auto
ref
=
new
QMultiObjectRegistryRef(engine,
"RepeatedMouseArea"
);
73
const
auto
repeaterObjects
= ref->objects();
74
for
(
auto
obj : repeaterObjects) {
75
connect(obj, SIGNAL(clicked(QQuickMouseEvent*)),
76
this
, SLOT(handleRepeaterObjectClicked()));
77
}
78
79
void
MyClass::handleRepeaterObjectClicked()
80
{
81
qDebug() <<
"Clicked rectangle index: "
<< sender()->property(
"repeaterIndex"
).toInt()
82
}
83
//! [5]
84
85
//! [6]
86
// Registering with attached property
87
Item {
88
id: itemToRegister
89
ObjectRegistry.key:
"MyItem"
90
}
91
92
// Registering with separate ObjectRegistry object
93
Item {
94
id: itemToRegister
95
ObjectRegistry {
96
target: itemToRegister
97
key:
"MyItem"
98
}
99
}
100
//! [6]
MouseArea
MouseArea
[0]
Definition
doc_src_objectregistry.cpp:6
Repeater
Repeater
[2]
Definition
doc_src_objectregistry.cpp:36
repeaterObjects
const auto repeaterObjects
Definition
doc_src_objectregistry.cpp:73
object
const auto object
Definition
doc_src_objectregistry.cpp:29
connect
connect(manager, &QNetworkAccessManager::finished, this, &MyClass::replyFinished)
QImageData::ref
QAtomicInt ref
Definition
qimage_p.h:44
qtdeclarative
src
qmldesignsupport
doc
snippets
doc_src_objectregistry.cpp
Generated on
for Qt by
1.18.0