QQmlListReference Class
QQmlListReference 类允许对QQmlListProperty 属性进行操作。更多
Header: | #include <QQmlListReference> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Qml) target_link_libraries(mytarget PRIVATE Qt6::Qml) |
qmake: | QT += qml |
公共函数
QQmlListReference() | |
(since 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 * | object() const |
bool | removeLast() const |
bool | replace(qsizetype index, QObject *object) const |
(since 6.2) qsizetype | size() const |
bool | operator==(const QQmlListReference &other) const |
详细说明
QQmlListReference 允许 C++ 程序以简单且类型安全的方式读取 QML 列表属性并为其赋值。与使用QQmlListProperty 本身相比,其主要优势在于类型清除:QQmlListReference 不是模板,但可用于任何类型的 QQmlListProperties。此外,它还会监视所有者对象是否被删除,如果所有者已被删除,则不再允许访问QQmlListProperty 。
您可以从一个对象和一个属性名称或从QVariant 创建一个 QQmlListReference。这两者是等价的:
QQmlListReference ref1(object, "children"); const QVariant var = object->property("children"); QQmlListReference ref2(var);
并非所有 QQmlListReferences 都支持所有操作。通过canAppend(),canAt(),canClear() 和canCount() 等方法,程序可以查询给定属性是否支持某种操作。这些方法的可用性取决于底层QQmlListProperty 所实现的方法。在通过手动传递访问函数来构建QQmlListProperty 时,您可以通过向某些函数传递 nullptr 来限制对列表的访问。QQmlListReference 会识别这些函数,并将其报告为不可用。
QQmlListReferenceQQmlListReference 是类型安全的。只有派生自正确基类的QObjects 才能添加到列表中。listElementType() 方法可用于查询可添加的QObject 类型的QMetaObject 。向列表属性添加错误类型对象的尝试将失败。
与其他列表一样,在通过索引访问列表元素时,调用者有责任确保不会请求超出范围的元素。为此,在调用at() 之前,请使用count() 方法。
成员函数文档
QQmlListReference::QQmlListReference()
构造一个无效实例。
[explicit, since 6.1]
QQmlListReference::QQmlListReference(const QVariant &variant)
从包含QQmlListProperty 的QVariant variant 构造一个 QQmlListReference。如果variant 不包含列表属性,则会创建一个无效的 QQmlListReference。如果在创建引用后,拥有列表属性的对象被销毁,它将自动变为无效。也就是说,即使对象被删除,持有 QQmlListReference 实例也是安全的。
此函数在 Qt 6.1 中引入。
QQmlListReference::QQmlListReference(QObject *object, const char *property)
为object 的property 构建一个 QQmlListReference。如果property 不是列表属性,则会创建一个无效的 QQmlListReference。如果object 在引用创建后被销毁,它将自动变为无效。也就是说,即使object 被删除,保留 QQmlListReference 实例也是安全的。
bool QQmlListReference::append(QObject *object) const
将object 追加到列表中。如果操作成功,则返回 true,否则返回 false。
另请参阅 canAppend()。
QObject *QQmlListReference::at(qsizetype index) const
返回位于index 的 list 元素,如果操作失败,则返回nullptr
。
另请参阅 canAt().
bool QQmlListReference::canAppend() const
如果可以追加列表属性,则返回 true,否则返回 false。如果引用无效,则返回 false。
另请参阅 append()。
bool QQmlListReference::canAt() const
如果可以通过索引查询列表属性,则返回 true,否则返回 false。如果引用无效,则返回 false。
另请参见 at()。
bool QQmlListReference::canClear() const
如果可以清除列表属性,则返回 true,否则返回 false。如果引用无效,则返回 false。
另请参见 clear()。
bool QQmlListReference::canCount() const
如果可以查询列表属性的元素计数,则返回 true,否则返回 false。如果引用无效,则返回 false。
另请参见 count()。
bool QQmlListReference::canRemoveLast() const
如果最后一个项目可以从列表属性中移除,则返回 true,否则返回 false。如果引用无效,则返回 false。
另请参阅 removeLast().
bool QQmlListReference::canReplace() const
如果可以替换列表属性中的项目,则返回 true,否则返回 false。如果引用无效,则返回 false。
另请参见 replace()。
bool QQmlListReference::clear() const
清除列表。如果操作成功,则返回 true,否则返回 false。
另请参阅 canClear()。
qsizetype QQmlListReference::count() const
返回列表中的条目数,如果操作失败,则返回 0。
bool QQmlListReference::isManipulable() const
如果at()、count()、append() 以及clear() 或removeLast() 已执行,则返回 true,这样就可以对列表进行操作。
请注意,replace() 和removeLast() 可以通过存储所有项目并使用clear() 和append() 重建列表来模拟。因此,要使列表具有可操作性,并非必须使用它们。此外,还可以使用removeLast() 来模拟clear()。
另请参见 isReadable()、at()、count()、append()、clear()、replace() 和removeLast()。
bool QQmlListReference::isReadable() const
如果at() 和count() 已实现,则返回 true,这样您就可以访问这些元素。
另请参阅 isManipulable()、at() 和count()。
bool QQmlListReference::isValid() const
如果实例指向有效的列表属性,则返回 true,否则返回 false。
const QMetaObject *QQmlListReference::listElementType() const
返回存储在 list 属性中元素的QMetaObject ,如果引用无效,则返回nullptr
。
QMetaObject 可用于提前确定给定实例是否可以添加到列表中。如果在构建时没有传递引擎,则可能返回 nullptr。
QObject *QQmlListReference::object() const
返回列表属性的对象。如果引用无效,则返回nullptr
。
bool QQmlListReference::removeLast() const
删除列表中的最后一个项目。如果操作成功,则返回 true,否则返回 false。
另请参阅 canRemoveLast().
bool QQmlListReference::replace(qsizetype index, QObject *object) const
用object 替换列表中index 处的项目。如果操作成功,则返回 true,否则返回 false。
另请参阅 canReplace().
[since 6.2]
qsizetype QQmlListReference::size() const
返回列表中的条目数,如果操作失败则返回 0。
此函数在 Qt 6.2 中引入。
bool QQmlListReference::operator==(const QQmlListReference &other) const
将QQmlListReference 与other 进行比较,如果两者相等,则返回true
。只有通过复制赋值或复制构造从另一个对象创建的引用才被视为相等。
注意: 对同一对象独立创建的引用不视为相等。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.