The QQmlListReference class allows the manipulation of QQmlListProperty 特性。 更多...
| 头: |
#include <QQmlListReference>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
|
| qmake: |
QT += qml
|
| QQmlListReference () | |
(从 6.1 起)
|
QQmlListReference (const QVariant & variant ) |
| QQmlListReference (QObject * object , const char * property ) | |
| bool | append (QObject * object ) const |
| QObject * | at (qsizetype index ) const |
| bool | canAppend () const |
| bool | canAt () const |
| bool | canClear () const |
| bool | canCount () const |
| bool | canRemoveLast () const |
| bool | canReplace () const |
| bool | clear () const |
| qsizetype | count () const |
| bool | isManipulable () const |
| bool | isReadable () const |
| bool | isValid () const |
| const QMetaObject * | listElementType () const |
| QObject * | 对象 () const |
| bool | removeLast () const |
| bool | replace (qsizetype index , QObject * object ) const |
(从 6.2 起)
qsizetype
|
size () const |
| bool | operator== (const QQmlListReference & other ) const |
QQmlListReference allows C++ programs to read from, and assign values to a QML list property in a simple and type-safe way. The main advantage over using QQmlListProperty itself is its type erasure: QQmlListReference is not a template, but can be used for QQmlListProperties of any type. Furthermore it watches the owner object for deletion and does not allow the QQmlListProperty to be accessed anymore if its owner has been deleted.
You can create a QQmlListReference from either an object and a property name or from a QVariant . These two are equivalent:
QQmlListReference ref1(object, "children"); const QVariant var = object->property("children"); QQmlListReference ref2(var);
Not all QQmlListReferences support all operations. Methods like canAppend (), canAt (), canClear (),和 canCount () allow programs to query whether an operation is supported on a given property. The availability of the methods depends on the methods implemented by the underlying QQmlListProperty . When constructing a QQmlListProperty by manually passing the accessor functions you can restrict access to the list by passing nullptr to some of them. QQmlListReference will recognize those and report them as unavailable.
QQmlListReference s are type-safe. Only QObject s that derive of the correct base class can be added to the list. The listElementType () method can be used to query the QMetaObject 的 QObject type that can be added. Attempting to add objects of an incorrect type to a list property will fail.
Like with other lists, when accessing a list element by index, it is the callers responsibility to ensure that it does not request an out of range element. Use the count () method before calling at () to this effect.
Constructs an invalid instance.
[explicit, since 6.1]
QQmlListReference::
QQmlListReference
(const
QVariant
&
variant
)
Constructs a QQmlListReference from a QVariant variant 包含 QQmlListProperty 。若 variant does not contain a list property, an invalid QQmlListReference is created. If the object owning the list property is destroyed after the reference is constructed, it will automatically become invalid. That is, it is safe to hold QQmlListReference instances even after the object is deleted.
该函数在 Qt 6.1 引入。
Constructs a QQmlListReference for object 's property 。若 property is not a list property, an invalid QQmlListReference is created. If object is destroyed after the reference is constructed, it will automatically become invalid. That is, it is safe to hold QQmlListReference instances even after object 被删除。
追加 object to the list. Returns true if the operation succeeded, otherwise false.
另请参阅 canAppend ().
Returns the list element at
index
,或
nullptr
if the operation failed.
另请参阅 canAt ().
Returns true if the list property can be appended to, otherwise false. Returns false if the reference is invalid.
另请参阅 append ().
Returns true if the list property can queried by index, otherwise false. Returns false if the reference is invalid.
另请参阅 at ().
Returns true if the list property can be cleared, otherwise false. Returns false if the reference is invalid.
另请参阅 clear ().
Returns true if the list property can be queried for its element count, otherwise false. Returns false if the reference is invalid.
另请参阅 count ().
Returns true if the last item can be removed from the list property, otherwise false. Returns false if the reference is invalid.
另请参阅 removeLast ().
Returns true if items in the list property can be replaced, otherwise false. Returns false if the reference is invalid.
另请参阅 replace ().
Clears the list. Returns true if the operation succeeded, otherwise false.
另请参阅 canClear ().
Returns the number of items in the list, or 0 if the operation failed.
返回 True 若 at (), count (), append (), and either clear () 或 removeLast () are implemented, so you can manipulate the list.
Mind that replace () 和 removeLast () can be emulated by stashing all items and rebuilding the list using clear () 和 append (). Therefore, they are not required for the list to be manipulable. Furthermore, clear () can be emulated using removeLast ().
另请参阅 isReadable (), at (), count (), append (), clear (), replace (),和 removeLast ().
返回 True 若 at () 和 count () are implemented, so you can access the elements.
另请参阅 isManipulable (), at (),和 count ().
Returns true if the instance refers to a valid list property, otherwise false.
返回
QMetaObject
for the elements stored in the list property, or
nullptr
if the reference is invalid.
The QMetaObject can be used ahead of time to determine whether a given instance can be added to a list. If you didn't pass an engine on construction this may return nullptr.
Returns the list property's object. Returns
nullptr
if the reference is invalid.
Removes the last item in the list. Returns true if the operation succeeded, otherwise false.
另请参阅 canRemoveLast ().
Replaces the item at index in the list with object . Returns true if the operation succeeded, otherwise false.
另请参阅 canReplace ().
[since 6.2]
qsizetype
QQmlListReference::
size
() const
Returns the number of items in the list, or 0 if the operation failed.
该函数在 Qt 6.2 引入。
Compares this
QQmlListReference
to
other
,并返回
true
if they are equal. The two are only considered equal if one was created from the other via copy assignment or copy construction.
注意: Independently created references to the same object are not considered to be equal.