本页内容

QQmlListReference Class

QQmlListReference 类允许对QQmlListProperty 的属性进行操作。更多内容...

头文件: #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);

并非所有 QQmlListReference 都支持所有操作。诸如canAppend()、canAt()、canClear() 和canCount() 等方法允许程序查询给定属性是否支持某项操作。 这些方法的可用性取决于底层QQmlListProperty 所实现的方法。在通过手动传递访问器函数来构造QQmlListProperty 时,您可以通过向其中某些函数传递nullptr来限制对列表的访问。QQmlListReference会识别这些情况,并将其报告为不可用。

QQmlListReference这些列表是类型安全的。只有继承自正确基类的QObject才能被添加到列表中。可以使用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 的列表元素;如果操作失败,则返回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() 重建列表来模拟。因此,这些方法并非列表可操作性的必要条件。此外,clear() 也可以通过removeLast() 来模拟。

另请参阅 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

返回列表属性中存储的元素的QMetaObject ,如果引用无效,则返回nullptr 。

QMetaObject 可预先用于确定给定实例是否可以添加到列表中。如果在构造时未传递引擎,此方法可能会返回 nullptr。

QObject *QQmlListReference::object() const

返回列表属性的对象。如果引用无效,则返回nullptr 。

bool QQmlListReference::removeLast() const

删除列表中的最后一个元素。如果操作成功,则返回 true,否则返回 false。

另请参阅 canRemoveLast()。

bool QQmlListReference::replace(qsizetype index, QObject *object) const

将列表中位于index 的项替换为object 。如果操作成功,则返回 true;否则返回 false。

另请参阅 canReplace()。

[since 6.2] qsizetype QQmlListReference::size() const

返回列表中的项目数量;如果操作失败,则返回 0。

该函数于 Qt 6.2 中引入。

bool QQmlListReference::operator==(const QQmlListReference &other) const

将此对象QQmlListReference 与other 进行比较,若两者相等,则返回true 。只有当其中一个对象是通过复制赋值或复制构造从另一个对象创建而来的时,才认为它们相等。

注意:独立 创建的、指向同一对象的引用不被视为相等。

© 2026 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.