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
qqmllist.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
"qqmllist.h"
6
#
include
"qqmllist_p.h"
7
8
#
include
<
private
/
qqmlcontextdata_p
.
h
>
9
#
include
<
private
/
qqmlproperty_p
.
h
>
10
#
include
<
private
/
qqmlpropertycache_p
.
h
>
11
12
QT_BEGIN_NAMESPACE
13
14
static
bool
isObjectCompatible
(QObject *object,
QQmlListReferencePrivate
*d)
15
{
16
if
(object) {
17
const
QQmlMetaObject elementType = d
->
elementType
(
)
;
18
if
(elementType.isNull() || !QQmlMetaObject::canConvert(object, elementType))
19
return
false
;
20
}
21
return
true
;
22
}
23
24
QQmlListReferencePrivate
::
QQmlListReferencePrivate
()
25
:
refCount
(1)
26
{
27
}
28
29
QQmlListReference
QQmlListReferencePrivate
::init(
30
const
QQmlListProperty
<QObject> &prop, QMetaType propType)
31
{
32
QQmlListReference
rv;
33
34
if
(!prop.object)
return
rv;
35
36
rv.d =
new
QQmlListReferencePrivate;
37
rv.d->object = prop.object;
38
rv.d->property = prop;
39
rv.d->propertyType = propType;
40
41
return
rv;
42
}
43
44
void
QQmlListReferencePrivate
::
addref
()
45
{
46
Q_ASSERT(
refCount
> 0);
47
++
refCount
;
48
}
49
50
void
QQmlListReferencePrivate
::
release
()
51
{
52
Q_ASSERT(
refCount
> 0);
53
--
refCount
;
54
if
(!
refCount
)
55
delete
this
;
56
}
57
58
/*!
59
\class QQmlListReference
60
\since 5.0
61
\inmodule QtQml
62
\brief The QQmlListReference class allows the manipulation of \l QQmlListProperty properties.
63
64
QQmlListReference allows C++ programs to read from, and assign values to a QML list property in a
65
simple and type-safe way. The main advantage over using \l QQmlListProperty itself is its type
66
erasure: QQmlListReference is not a template, but can be used for QQmlListProperties of any type.
67
Furthermore it watches the owner object for deletion and does not allow the \l QQmlListProperty
68
to be accessed anymore if its owner has been deleted.
69
70
You can create a QQmlListReference from either an object and a property name or from a QVariant.
71
These two are equivalent:
72
73
\code
74
QQmlListReference ref1(object, "children");
75
76
const QVariant var = object->property("children");
77
QQmlListReference ref2(var);
78
\endcode
79
80
Not all QQmlListReferences support all operations. Methods like canAppend(),
81
canAt(), canClear(), and canCount() allow programs to query whether an
82
operation is supported on a given property. The availability of the methods
83
depends on the methods implemented by the underlying \l QQmlListProperty. When
84
constructing a \l QQmlListProperty by manually passing the accessor
85
functions you can restrict access to the list by passing nullptr to some of them.
86
QQmlListReference will recognize those and report them as unavailable.
87
88
\l{QQmlListReference}s are type-safe. Only \l{QObject}s that derive of the
89
correct base class can be added to the list. The listElementType() method can be
90
used to query the \l QMetaObject of the \l QObject type that can be added.
91
Attempting to add objects of an incorrect type to a list property will fail.
92
93
Like with other lists, when accessing a list element by index, it is the
94
callers responsibility to ensure that it does not request an out of range
95
element. Use the count() method before calling at() to this effect.
96
*/
97
98
/*!
99
\fn bool QQmlListReference::operator==(const QQmlListReference &other) const
100
101
Compares this QQmlListReference to \a other, and returns \c true if they are
102
equal. The two are only considered equal if one was created from the other
103
via copy assignment or copy construction.
104
105
\note Independently created references to the same object are not considered
106
to be equal.
107
*/
108
109
/*!
110
Constructs an invalid instance.
111
*/
112
QQmlListReference::QQmlListReference()
113
: d(
nullptr
)
114
{
115
}
116
117
#
if
QT_DEPRECATED_SINCE
(
6
,
4
)
118
/*!
119
\since 6.1
120
\obsolete [6.4] Use the constructors without QQmlEngine argument instead.
121
122
Constructs a QQmlListReference from a QVariant \a variant containing a QQmlListProperty. If
123
\a variant does not contain a list property, an invalid QQmlListReference is created. If the object
124
owning the list property is destroyed after the reference is constructed, it will automatically
125
become invalid. That is, it is safe to hold QQmlListReference instances even after the object is
126
deleted.
127
128
The \a engine is unused.
129
*/
130
QQmlListReference::QQmlListReference(
const
QVariant &variant, [[maybe_unused]] QQmlEngine *engine)
131
: QQmlListReference(variant)
132
{}
133
134
/*!
135
\obsolete [6.4] Use the constructors without QQmlEngine argument instead.
136
137
Constructs a QQmlListReference for \a object's \a property. If \a property is not a list
138
property, an invalid QQmlListReference is created. If \a object is destroyed after
139
the reference is constructed, it will automatically become invalid. That is, it is safe to hold
140
QQmlListReference instances even after \a object is deleted.
141
142
The \a engine is unused.
143
*/
144
QQmlListReference::QQmlListReference(QObject *object,
const
char
*property,
145
[[maybe_unused]] QQmlEngine *engine)
146
: QQmlListReference(object, property)
147
{}
148
#
endif
149
150
/*!
151
\since 6.1
152
153
Constructs a QQmlListReference from a QVariant \a variant containing a QQmlListProperty. If
154
\a variant does not contain a list property, an invalid QQmlListReference is created. If the object
155
owning the list property is destroyed after the reference is constructed, it will automatically
156
become invalid. That is, it is safe to hold QQmlListReference instances even after the object is
157
deleted.
158
*/
159
QQmlListReference::QQmlListReference(
const
QVariant &variant)
160
: d(
nullptr
)
161
{
162
const
QMetaType t = variant.metaType();
163
if
(!(t.flags() & QMetaType::IsQmlList))
164
return
;
165
166
d =
new
QQmlListReferencePrivate;
167
d->propertyType = t;
168
169
d->property.~QQmlListProperty();
170
t.construct(&d->property, variant.constData());
171
172
d->object = d->property.object;
173
}
174
175
/*!
176
Constructs a QQmlListReference for \a object's \a property. If \a property is not a list
177
property, an invalid QQmlListReference is created. If \a object is destroyed after
178
the reference is constructed, it will automatically become invalid. That is, it is safe to hold
179
QQmlListReference instances even after \a object is deleted.
180
*/
181
QQmlListReference::QQmlListReference(QObject *object,
const
char
*property)
182
: d(
nullptr
)
183
{
184
if
(!object || !property)
return
;
185
186
QQmlPropertyData local;
187
const
QQmlPropertyData *data =
188
QQmlPropertyCache::property(object, QLatin1String(property),
nullptr
, &local);
189
190
if
(!data || !data->isQList())
return
;
191
192
d =
new
QQmlListReferencePrivate;
193
d->object = object;
194
d->propertyType = data->propType();
195
196
void
*args[] = { &d->property,
nullptr
};
197
QMetaObject::metacall(object, QMetaObject::ReadProperty, data->coreIndex(), args);
198
}
199
200
/*! \internal */
201
QQmlListReference::QQmlListReference(
const
QQmlListReference &o)
202
: d(o.d)
203
{
204
if
(d) d->addref();
205
}
206
207
/*! \internal */
208
QQmlListReference &QQmlListReference::operator=(
const
QQmlListReference &o)
209
{
210
if
(o.d) o.d->addref();
211
if
(d) d->release();
212
d = o.d;
213
return
*
this
;
214
}
215
216
/*! \internal */
217
QQmlListReference::~QQmlListReference()
218
{
219
if
(d) d->release();
220
}
221
222
/*!
223
Returns true if the instance refers to a valid list property, otherwise false.
224
*/
225
bool
QQmlListReference::isValid()
const
226
{
227
return
d && d->object;
228
}
229
230
/*!
231
Returns the list property's object. Returns \nullptr if the reference is invalid.
232
*/
233
QObject *QQmlListReference::object()
const
234
{
235
if
(isValid())
return
d->object;
236
else
return
nullptr
;
237
}
238
239
/*!
240
Returns the QMetaObject for the elements stored in the list property,
241
or \nullptr if the reference is invalid.
242
243
The QMetaObject can be used ahead of time to determine whether a given instance can be added
244
to a list. If you didn't pass an engine on construction this may return nullptr.
245
*/
246
const
QMetaObject *QQmlListReference::listElementType()
const
247
{
248
return
isValid() ? d->elementType() :
nullptr
;
249
}
250
251
/*!
252
Returns true if the list property can be appended to, otherwise false. Returns false if the
253
reference is invalid.
254
255
\sa append()
256
*/
257
bool
QQmlListReference::canAppend()
const
258
{
259
return
(isValid() && d->property.append);
260
}
261
262
/*!
263
Returns true if the list property can queried by index, otherwise false. Returns false if the
264
reference is invalid.
265
266
\sa at()
267
*/
268
bool
QQmlListReference::canAt()
const
269
{
270
return
(isValid() && d->property.at);
271
}
272
273
/*!
274
Returns true if the list property can be cleared, otherwise false. Returns false if the
275
reference is invalid.
276
277
\sa clear()
278
*/
279
bool
QQmlListReference::canClear()
const
280
{
281
return
(isValid() && d->property.clear);
282
}
283
284
/*!
285
Returns true if the list property can be queried for its element count, otherwise false.
286
Returns false if the reference is invalid.
287
288
\sa count()
289
*/
290
bool
QQmlListReference::canCount()
const
291
{
292
return
(isValid() && d->property.count);
293
}
294
295
/*!
296
Returns true if items in the list property can be replaced, otherwise false.
297
Returns false if the reference is invalid.
298
299
\sa replace()
300
*/
301
bool
QQmlListReference::canReplace()
const
302
{
303
return
(isValid() && d->property.replace);
304
}
305
306
/*!
307
Returns true if the last item can be removed from the list property, otherwise false.
308
Returns false if the reference is invalid.
309
310
\sa removeLast()
311
*/
312
bool
QQmlListReference::canRemoveLast()
const
313
{
314
return
(isValid() && d->property.removeLast);
315
}
316
317
/*!
318
Return true if at(), count(), append(), and either clear() or removeLast()
319
are implemented, so you can manipulate the list.
320
321
Mind that replace() and removeLast() can be emulated by stashing all
322
items and rebuilding the list using clear() and append(). Therefore,
323
they are not required for the list to be manipulable. Furthermore,
324
clear() can be emulated using removeLast().
325
326
\sa isReadable(), at(), count(), append(), clear(), replace(), removeLast()
327
*/
328
bool
QQmlListReference::isManipulable()
const
329
{
330
return
(isValid()
331
&& d->property.append
332
&& d->property.count
333
&& d->property.at
334
&& d->property.clear);
335
}
336
337
338
/*!
339
Return true if at() and count() are implemented, so you can access the elements.
340
341
\sa isManipulable(), at(), count()
342
*/
343
bool
QQmlListReference::isReadable()
const
344
{
345
return
(isValid() && d->property.count && d->property.at);
346
}
347
348
/*!
349
Appends \a object to the list. Returns true if the operation succeeded, otherwise false.
350
351
\sa canAppend()
352
*/
353
bool
QQmlListReference::append(QObject *object)
const
354
{
355
if
(!canAppend())
return
false
;
356
357
if
(!isObjectCompatible(object, d))
358
return
false
;
359
360
d->property.append(&d->property, object);
361
362
return
true
;
363
}
364
365
/*!
366
Returns the list element at \a index, or \nullptr if the operation failed.
367
368
\sa canAt()
369
*/
370
QObject *QQmlListReference::at(qsizetype index)
const
371
{
372
if
(!canAt())
return
nullptr
;
373
374
return
d->property.at(&d->property, index);
375
}
376
377
/*!
378
Clears the list. Returns true if the operation succeeded, otherwise false.
379
380
\sa canClear()
381
*/
382
bool
QQmlListReference::clear()
const
383
{
384
if
(!canClear())
return
false
;
385
386
d->property.clear(&d->property);
387
388
return
true
;
389
}
390
391
/*!
392
Returns the number of items in the list, or 0 if the operation failed.
393
*/
394
qsizetype QQmlListReference::count()
const
395
{
396
if
(!canCount())
return
0;
397
398
return
d->property.count(&d->property);
399
}
400
401
/*!
402
\fn qsizetype QQmlListReference::size() const
403
\since 6.2
404
Returns the number of items in the list, or 0 if the operation failed.
405
*/
406
407
/*!
408
Replaces the item at \a index in the list with \a object.
409
Returns true if the operation succeeded, otherwise false.
410
411
\sa canReplace()
412
*/
413
bool
QQmlListReference::replace(qsizetype index, QObject *object)
const
414
{
415
if
(!canReplace())
416
return
false
;
417
418
if
(!isObjectCompatible(object, d))
419
return
false
;
420
421
d->property.replace(&d->property, index, object);
422
return
true
;
423
}
424
425
/*!
426
Removes the last item in the list.
427
Returns true if the operation succeeded, otherwise false.
428
429
\sa canRemoveLast()
430
*/
431
bool
QQmlListReference::removeLast()
const
432
{
433
if
(!canRemoveLast())
434
return
false
;
435
436
d->property.removeLast(&d->property);
437
return
true
;
438
}
439
440
/*!
441
\class QQmlListProperty
442
\since 5.0
443
\inmodule QtQml
444
\brief The QQmlListProperty class allows applications to expose list-like
445
properties of QObject-derived classes to QML.
446
447
QML has many list properties, where more than one object value can be assigned.
448
The use of a list property from QML looks like this:
449
450
\code
451
FruitBasket {
452
fruit: [
453
Apple {},
454
Orange{},
455
Banana{}
456
]
457
}
458
\endcode
459
460
The QQmlListProperty encapsulates a group of function pointers that represent the
461
set of actions QML can perform on the list - adding items, retrieving items and
462
clearing the list. In the future, additional operations may be supported. All
463
list properties must implement the append operation, but the rest are optional.
464
465
To provide a list property, a C++ class must implement the operation callbacks,
466
and then return an appropriate QQmlListProperty value from the property getter.
467
List properties should have no setter. In the example above, the Q_PROPERTY()
468
declarative will look like this:
469
470
\code
471
Q_PROPERTY(QQmlListProperty<Fruit> fruit READ fruit)
472
\endcode
473
474
QML list properties are type-safe. The template parameter \a T specifies the
475
QObject-derived type that can be stored in the list. In this case \c {Fruit}
476
is a QObject type that \c {Apple}, \c {Orange} and \c {Banana} all derive from.
477
478
You can use \l{QQmlListReference} to manipulate a QQmlListProperty from C++ using
479
a slightly more ergonomic API, at the cost of some overhead.
480
481
\sa {Chapter 5: Using List Property Types}, QQmlListReference
482
*/
483
484
/*!
485
\macro QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
486
\relates QQmlListProperty
487
488
This macro defines the behavior of the list properties of this class to Append.
489
When assigning the property in a derived type, the values are appended
490
to those of the base class. This is the default behavior.
491
492
\snippet code/src_qml_qqmllist.cpp 0
493
494
\sa QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT
495
\sa QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE
496
\sa {Defining Object Types through QML Documents}
497
*/
498
499
/*!
500
\macro QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT
501
\relates QQmlListProperty
502
503
This macro defines the behavior of the list properties of this class to
504
ReplaceIfNotDefault.
505
When assigning the property in a derived type, the values replace those of
506
the base class unless it's the default property.
507
In the case of the default property, values are appended to those of the base class.
508
509
\snippet code/src_qml_qqmllist.cpp 1
510
511
\sa QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
512
\sa QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE
513
\sa {Defining Object Types through QML Documents}
514
*/
515
516
/*!
517
\macro QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE
518
\relates QQmlListProperty
519
520
This macro defines the behavior of the list properties of this class to Replace.
521
When assigning the property in a derived type, the values replace those
522
of the base class.
523
524
\snippet code/src_qml_qqmllist.cpp 2
525
526
\sa QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
527
\sa QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT
528
\sa {Defining Object Types through QML Documents}
529
*/
530
531
/*!
532
\fn template<typename T> QQmlListProperty<T>::QQmlListProperty()
533
\internal
534
*/
535
536
/*!
537
\fn template<typename T> QQmlListProperty<T>::QQmlListProperty(QObject *object, QList<T *> *list)
538
\since 5.15
539
540
Convenience constructor for making a QQmlListProperty value from an existing
541
QList \a list. The \a object owning the list and the \a list itself must be
542
provided and kept alive as long as you are holding a QQmlListProperty referring
543
to them.
544
545
This is the easiest and safest way to provide a QQmlListProperty backed by a QList
546
and should be used in most cases. A typical invocation looks like this:
547
548
\snippet tutorials/extending-qml/chapter5-listproperties/piechart.cpp 0
549
*/
550
551
/*!
552
\fn template<typename T> QQmlListProperty<T>::QQmlListProperty(QObject *object, void *data,
553
CountFunction count, AtFunction at)
554
555
Construct a readonly QQmlListProperty from a set of operation functions
556
\a count and \a at. An opaque \a data handle may be passed which can be
557
accessed from within the operation functions. The list property
558
remains valid while the \a object owning the list property exists.
559
*/
560
561
/*!
562
\fn template<typename T> QQmlListProperty<T>::QQmlListProperty(QObject *object, void *data, AppendFunction append,
563
CountFunction count, AtFunction at,
564
ClearFunction clear)
565
566
Construct a QQmlListProperty from a set of operation functions \a append,
567
\a count, \a at, and \a clear. An opaque \a data handle may be passed which
568
can be accessed from within the operation functions. The list property
569
remains valid while the \a object owning the list property exists.
570
571
Null pointers can be passed for any function. If any null pointers are passed in, the list
572
will be neither designable nor alterable by the debugger. It is recommended to provide valid
573
pointers for all functions.
574
575
\note The resulting QQmlListProperty will synthesize the removeLast() and
576
replace() methods using \a count, \a at, \a clear, and \a append if all of those
577
are given. This is slow. If you intend to manipulate the list beyond clearing it,
578
you should explicitly provide these methods.
579
*/
580
581
/*!
582
\fn template<typename T> QQmlListProperty<T>::QQmlListProperty(
583
QObject *object, void *data, AppendFunction append, CountFunction count,
584
AtFunction at, ClearFunction clear, ReplaceFunction replace,
585
RemoveLastFunction removeLast)
586
587
Construct a QQmlListProperty from a set of operation functions \a append,
588
\a count, \a at, \a clear, \a replace, and \a removeLast. An opaque \a data handle
589
may be passed which can be accessed from within the operation functions. The
590
list property remains valid while the \a object owning the list property
591
exists.
592
593
Null pointers can be passed for any function, causing the respective function to
594
be synthesized using the others, if possible. QQmlListProperty can synthesize
595
\list
596
\li \a clear using \a count and \a removeLast
597
\li \a replace using \a count, \a at, \a clear, and \a append
598
\li \a replace using \a count, \a at, \a removeLast, and \a append
599
\li \a removeLast using \a count, \a at, \a clear, and \a append
600
\endlist
601
if those are given. This is slow, but if your list does not natively provide
602
faster options for these primitives, you may want to use the synthesized ones.
603
604
Furthermore, if either of \a count, \a at, \a append, and \a clear are neither
605
given explicitly nor synthesized, the list will be neither designable nor
606
alterable by the debugger. It is recommended to provide enough valid pointers
607
to avoid this situation.
608
*/
609
610
/*!
611
\typedef QQmlListProperty::AppendFunction
612
613
Synonym for \c {void (*)(QQmlListProperty<T> *property, T *value)}.
614
615
Append the \a value to the list \a property.
616
*/
617
618
/*!
619
\typedef QQmlListProperty::CountFunction
620
621
Synonym for \c {qsizetype (*)(QQmlListProperty<T> *property)}.
622
623
Return the number of elements in the list \a property.
624
*/
625
626
/*!
627
\fn template<typename T> bool QQmlListProperty<T>::operator==(const QQmlListProperty &other) const
628
629
Returns true if this QQmlListProperty is equal to \a other, otherwise false.
630
*/
631
632
/*!
633
\typedef QQmlListProperty::AtFunction
634
635
Synonym for \c {T *(*)(QQmlListProperty<T> *property, qsizetype index)}.
636
637
Return the element at position \a index in the list \a property.
638
*/
639
640
/*!
641
\typedef QQmlListProperty::ClearFunction
642
643
Synonym for \c {void (*)(QQmlListProperty<T> *property)}.
644
645
Clear the list \a property.
646
*/
647
648
/*!
649
\typedef QQmlListProperty::ReplaceFunction
650
651
Synonym for \c {void (*)(QQmlListProperty<T> *property, qsizetype index, T *value)}.
652
653
Replace the element at position \a index in the list \a property with \a value.
654
*/
655
656
/*!
657
\typedef QQmlListProperty::RemoveLastFunction
658
659
Synonym for \c {void (*)(QQmlListProperty<T> *property)}.
660
661
Remove the last element from the list \a property.
662
*/
663
664
/*!
665
\variable QQmlListProperty::object
666
\brief This field holds the \e owner of the QQmlListProperty
667
668
When manually implementing the accessor methods, you may need to use this field
669
for retrieving the content of the manipulated list.
670
*/
671
672
/*!
673
\variable QQmlListProperty::data
674
\brief This field can hold an arbitrary data pointer
675
676
If you manually implement the accessor methods and need to store custom data,
677
you can pass an arbitrary pointer to the QQmlListProperty constructor and
678
retrieve it from the \e data field when accessing the same QQmlListProperty
679
later.
680
681
A \l{QQmlListProperty(QObject *object, QList<T *> *list)}{QQmlListProperty constructed from a QList pointer}
682
uses this field to store the pointer to the list itself, as it cannot directly
683
access the list contents from the owner.
684
685
*/
686
687
QT_END_NAMESPACE
QQmlListProperty
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition
qqmllist.h:25
QQmlListReferencePrivate
Definition
qqmllist_p.h:29
QQmlListReferencePrivate::refCount
int refCount
Definition
qqmllist_p.h:41
QQmlListReferencePrivate::addref
void addref()
Definition
qqmllist.cpp:44
QQmlListReferencePrivate::elementType
const QMetaObject * elementType()
Definition
qqmllist_p.h:47
QQmlListReferencePrivate::release
void release()
Definition
qqmllist.cpp:50
QQmlListReferencePrivate::QQmlListReferencePrivate
QQmlListReferencePrivate()
Definition
qqmllist.cpp:24
QQmlListReference
The QQmlListReference class allows the manipulation of \l QQmlListProperty properties.
Definition
qqmllist.h:190
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
isObjectCompatible
static QT_BEGIN_NAMESPACE bool isObjectCompatible(QObject *object, QQmlListReferencePrivate *d)
Definition
qqmllist.cpp:14
qtdeclarative
src
qml
qml
qqmllist.cpp
Generated on
for Qt by
1.18.0