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
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
6
7#include <private/qobject_p.h>
8#include <private/qqmlguard_p.h>
9
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 QtQml.Models since version 2.14 and part of QtQuick since version 2.0.
40 Importing Package via QtQuick is deprecated since Qt 5.14.
41
42 \sa {Qt Quick Examples - Views}, {Qt Qml}
43*/
44
45/*!
46 \qmlattachedproperty string QtQml.Models::Package::name
47 This attached property holds the name of an item within a Package.
48*/
49
50
52{
53public:
55
56 struct DataGuard : public QQmlGuard<QObject>
57 {
58 DataGuard(QObject *obj, QList<DataGuard> *l) : QQmlGuard<QObject>(DataGuard::objectDestroyedImpl, nullptr), list(l) { (QQmlGuard<QObject>&)*this = obj; }
60
61 private:
62 static void objectDestroyedImpl(QQmlGuardImpl *guard) {
63 auto This = static_cast<DataGuard *>(guard);
64 // we assume priv will always be destroyed after objectDestroyed calls
65 This->list->removeOne(*This);
66 }
67 };
68
70 static void data_append(QQmlListProperty<QObject> *prop, QObject *o) {
71 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
72 list->append(DataGuard(o, list));
73 }
74 static void data_clear(QQmlListProperty<QObject> *prop) {
75 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
76 list->clear();
77 }
78 static QObject *data_at(QQmlListProperty<QObject> *prop, qsizetype index) {
79 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
80 return list->at(index);
81 }
82 static qsizetype data_count(QQmlListProperty<QObject> *prop) {
83 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
84 return list->size();
85 }
86 static void data_replace(QQmlListProperty<QObject> *prop, qsizetype index, QObject *o) {
87 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
88 list->replace(index, DataGuard(o, list));
89 }
90 static void data_removeLast(QQmlListProperty<QObject> *prop) {
91 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
92 list->removeLast();
93 }
94};
95
97
98QQuickPackageAttached::QQuickPackageAttached(QObject *parent)
99: QObject(parent)
100{
101 attached.insert(parent, this);
102}
103
105{
106 attached.remove(parent());
107}
108
110{
111 return _name;
112}
113
114void QQuickPackageAttached::setName(const QString &n)
115{
116 _name = n;
117}
118
119QQuickPackage::QQuickPackage(QObject *parent)
120 : QObject(*(new QQuickPackagePrivate), parent)
121{
122}
123
124QQmlListProperty<QObject> QQuickPackage::data()
125{
126 Q_D(QQuickPackage);
127 return QQmlListProperty<QObject>(this, &d->dataList,
128 QQuickPackagePrivate::data_append,
129 QQuickPackagePrivate::data_count,
130 QQuickPackagePrivate::data_at,
131 QQuickPackagePrivate::data_clear,
132 QQuickPackagePrivate::data_replace,
133 QQuickPackagePrivate::data_removeLast);
134}
135
136bool QQuickPackage::hasPart(const QString &name)
137{
138 Q_D(QQuickPackage);
139 for (int ii = 0; ii < d->dataList.size(); ++ii) {
140 QObject *obj = d->dataList.at(ii);
141 QQuickPackageAttached *a = QQuickPackageAttached::attached.value(obj);
142 if (a && a->name() == name)
143 return true;
144 }
145 return false;
146}
147
148QObject *QQuickPackage::part(const QString &name)
149{
150 Q_D(QQuickPackage);
151 if (name.isEmpty() && !d->dataList.isEmpty())
152 return d->dataList.at(0);
153
154 for (int ii = 0; ii < d->dataList.size(); ++ii) {
155 QObject *obj = d->dataList.at(ii);
156 QQuickPackageAttached *a = QQuickPackageAttached::attached.value(obj);
157 if (a && a->name() == name)
158 return obj;
159 }
160
161 if (name == QLatin1String("default") && !d->dataList.isEmpty())
162 return d->dataList.at(0);
163
164 return nullptr;
165}
166
167QQuickPackageAttached *QQuickPackage::qmlAttachedProperties(QObject *o)
168{
169 return new QQuickPackageAttached(o);
170}
171
172
173
174QT_END_NAMESPACE
175
176#include "moc_qquickpackage_p.cpp"
void setName(const QString &n)
static QHash< QObject *, QQuickPackageAttached * > attached
\qmltype Package \nativetype QQuickPackage \inqmlmodule QtQml.Models
static void data_removeLast(QQmlListProperty< QObject > *prop)
static QObject * data_at(QQmlListProperty< QObject > *prop, qsizetype index)
static void data_append(QQmlListProperty< QObject > *prop, QObject *o)
static qsizetype data_count(QQmlListProperty< QObject > *prop)
static void data_clear(QQmlListProperty< QObject > *prop)
static void data_replace(QQmlListProperty< QObject > *prop, qsizetype index, QObject *o)
QList< DataGuard > dataList
DataGuard(QObject *obj, QList< DataGuard > *l)