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
qquickpackage.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant
4
5
#
include
"qquickpackage_p.h"
6
7
#
include
<
private
/
qobject_p
.
h
>
8
#
include
<
private
/
qqmlguard_p
.
h
>
9
10
QT_BEGIN_NAMESPACE
11
12
/*!
13
\qmltype Package
14
\nativetype QQuickPackage
15
\inqmlmodule QtQml.Models
16
\ingroup qtquick-models
17
\brief Specifies a collection of named items.
18
19
The Package type is used in conjunction with
20
DelegateModel to enable delegates with a shared context
21
to be provided to multiple views.
22
23
Any item within a Package may be assigned a name via the
24
\l{Package::name}{Package.name} attached property.
25
26
The example below creates a Package containing two named items;
27
\e list and \e grid. The third item in the package (the \l Rectangle) is parented to whichever
28
delegate it should appear in. This allows an item to move
29
between views.
30
31
\snippet package/Delegate.qml 0
32
33
These named items are used as the delegates by the two views who
34
reference the special \l{DelegateModel::parts} property to select
35
a model which provides the chosen delegate.
36
37
\snippet package/view.qml 0
38
39
\note Package is part of \l{QtQml.Models} since version 2.14 and
40
part of \l[QML]{QtQuick} since version 2.0.
41
Importing Package via \l[QML]{QtQuick} is deprecated since Qt 5.14.
42
43
\sa {Qt Quick Examples - Views}, {Qt Qml}
44
*/
45
46
/*!
47
\qmlattachedproperty string QtQml.Models::Package::name
48
This attached property holds the name of an item within a Package.
49
*/
50
51
52
class
QQuickPackagePrivate
:
public
QObjectPrivate
53
{
54
public
:
55
QQuickPackagePrivate
() {}
56
57
struct
DataGuard
:
public
QQmlGuard
<
QObject
>
58
{
59
DataGuard
(QObject *obj, QList<DataGuard> *l) :
QQmlGuard
<
QObject
>(
DataGuard
::
objectDestroyedImpl
,
nullptr
),
list
(
l
) { (QQmlGuard<QObject>&)*
this
= obj; }
60
QList
<
DataGuard
> *
list
;
61
62
private
:
63
static
void
objectDestroyedImpl(QQmlGuardImpl *guard) {
64
auto
This =
static_cast
<
DataGuard
*>(guard);
65
// we assume priv will always be destroyed after objectDestroyed calls
66
This->list->removeOne(*This);
67
}
68
};
69
70
QList
<
DataGuard
>
dataList
;
71
static
void
data_append
(QQmlListProperty<QObject> *prop, QObject *o) {
72
QList<DataGuard> *list =
static_cast
<QList<DataGuard> *>(prop->data);
73
list->append(
DataGuard
(o, list));
74
}
75
static
void
data_clear
(QQmlListProperty<QObject> *prop) {
76
QList<DataGuard> *list =
static_cast
<QList<DataGuard> *>(prop->data);
77
list->clear();
78
}
79
static
QObject
*
data_at
(QQmlListProperty<QObject> *prop, qsizetype index) {
80
QList<DataGuard> *list =
static_cast
<QList<DataGuard> *>(prop->data);
81
return
list->at(index);
82
}
83
static
qsizetype
data_count
(QQmlListProperty<QObject> *prop) {
84
QList<DataGuard> *list =
static_cast
<QList<DataGuard> *>(prop->data);
85
return
list->size();
86
}
87
static
void
data_replace
(QQmlListProperty<QObject> *prop, qsizetype index, QObject *o) {
88
QList<DataGuard> *list =
static_cast
<QList<DataGuard> *>(prop->data);
89
list->replace(index,
DataGuard
(o, list));
90
}
91
static
void
data_removeLast
(QQmlListProperty<QObject> *prop) {
92
QList<DataGuard> *list =
static_cast
<QList<DataGuard> *>(prop->data);
93
list->removeLast();
94
}
95
};
96
97
QHash
<
QObject
*,
QQuickPackageAttached
*>
QQuickPackageAttached
::
attached
;
98
99
QQuickPackageAttached
::QQuickPackageAttached(QObject *parent)
100
: QObject(parent)
101
{
102
attached.insert(parent,
this
);
103
}
104
105
QQuickPackageAttached
::~
QQuickPackageAttached
()
106
{
107
attached.remove(parent());
108
}
109
110
QString
QQuickPackageAttached
::
name
()
const
111
{
112
return
_name;
113
}
114
115
void
QQuickPackageAttached
::
setName
(
const
QString &n)
116
{
117
_name = n;
118
}
119
120
QQuickPackage::QQuickPackage(QObject *parent)
121
: QObject(*(
new
QQuickPackagePrivate), parent)
122
{
123
}
124
125
QQmlListProperty<QObject> QQuickPackage::data()
126
{
127
Q_D(QQuickPackage);
128
return
QQmlListProperty<QObject>(
this
, &d->dataList,
129
QQuickPackagePrivate::data_append,
130
QQuickPackagePrivate::data_count,
131
QQuickPackagePrivate::data_at,
132
QQuickPackagePrivate::data_clear,
133
QQuickPackagePrivate::data_replace,
134
QQuickPackagePrivate::data_removeLast);
135
}
136
137
bool
QQuickPackage::hasPart(
const
QString &name)
138
{
139
Q_D(QQuickPackage);
140
for
(
int
ii = 0; ii < d->dataList.size(); ++ii) {
141
QObject *obj = d->dataList.at(ii);
142
QQuickPackageAttached *a = QQuickPackageAttached::attached.value(obj);
143
if
(a && a->name() == name)
144
return
true
;
145
}
146
return
false
;
147
}
148
149
QObject *QQuickPackage::part(
const
QString &name)
150
{
151
Q_D(QQuickPackage);
152
if
(name.isEmpty() && !d->dataList.isEmpty())
153
return
d->dataList.at(0);
154
155
for
(
int
ii = 0; ii < d->dataList.size(); ++ii) {
156
QObject *obj = d->dataList.at(ii);
157
QQuickPackageAttached *a = QQuickPackageAttached::attached.value(obj);
158
if
(a && a->name() == name)
159
return
obj;
160
}
161
162
if
(name == QLatin1String(
"default"
) && !d->dataList.isEmpty())
163
return
d->dataList.at(0);
164
165
return
nullptr
;
166
}
167
168
QQuickPackageAttached *QQuickPackage::qmlAttachedProperties(QObject *o)
169
{
170
return
new
QQuickPackageAttached(o);
171
}
172
173
174
175
QT_END_NAMESPACE
176
177
#
include
"moc_qquickpackage_p.cpp"
QQuickPackageAttached
Definition
qquickpackage_p.h:51
QQuickPackageAttached::name
QString name() const
Definition
qquickpackage.cpp:110
QQuickPackageAttached::~QQuickPackageAttached
virtual ~QQuickPackageAttached()
Definition
qquickpackage.cpp:105
QQuickPackageAttached::setName
void setName(const QString &n)
Definition
qquickpackage.cpp:115
QQuickPackageAttached::attached
static QHash< QObject *, QQuickPackageAttached * > attached
Definition
qquickpackage_p.h:61
QQuickPackagePrivate
\qmltype Package \nativetype QQuickPackage \inqmlmodule QtQml.Models
Definition
qquickpackage.cpp:53
QQuickPackagePrivate::data_removeLast
static void data_removeLast(QQmlListProperty< QObject > *prop)
Definition
qquickpackage.cpp:91
QQuickPackagePrivate::data_at
static QObject * data_at(QQmlListProperty< QObject > *prop, qsizetype index)
Definition
qquickpackage.cpp:79
QQuickPackagePrivate::data_append
static void data_append(QQmlListProperty< QObject > *prop, QObject *o)
Definition
qquickpackage.cpp:71
QQuickPackagePrivate::data_count
static qsizetype data_count(QQmlListProperty< QObject > *prop)
Definition
qquickpackage.cpp:83
QQuickPackagePrivate::QQuickPackagePrivate
QQuickPackagePrivate()
Definition
qquickpackage.cpp:55
QQuickPackagePrivate::data_clear
static void data_clear(QQmlListProperty< QObject > *prop)
Definition
qquickpackage.cpp:75
QQuickPackagePrivate::data_replace
static void data_replace(QQmlListProperty< QObject > *prop, qsizetype index, QObject *o)
Definition
qquickpackage.cpp:87
QQuickPackagePrivate::dataList
QList< DataGuard > dataList
Definition
qquickpackage.cpp:70
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QQuickPackagePrivate::DataGuard
Definition
qquickpackage.cpp:58
QQuickPackagePrivate::DataGuard::DataGuard
DataGuard(QObject *obj, QList< DataGuard > *l)
Definition
qquickpackage.cpp:59
QQuickPackagePrivate::DataGuard::list
QList< DataGuard > * list
Definition
qquickpackage.cpp:60
qtdeclarative
src
qmlmodels
qquickpackage.cpp
Generated on
for Qt by
1.18.0