QAssociativeIterable Class
QAssociativeIterable 类是QVariant 中关联容器的可迭代接口。
头文件: | #include <QAssociativeIterable> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
继承: | QIterable |
公共类型
公共函数
bool | containsKey(const QVariant &key) |
QAssociativeIterable::const_iterator | find(const QVariant &key) const |
void | insertKey(const QVariant &key) |
QAssociativeIterable::iterator | mutableFind(const QVariant &key) |
void | removeKey(const QVariant &key) |
void | setValue(const QVariant &key, const QVariant &mapped) |
QVariant | value(const QVariant &key) const |
详细说明
该类允许使用多种方法访问QVariant 中关联容器的元素。如果可以将QVariant 转换为QVariantHash 或QVariantMap ,或者已经注册了自定义可变视图,则可以从 中提取 QAssociativeIterable 的实例。
QHash<int、 QString>mapping; mapping.insert(7, "Seven"); mapping.insert(11, "Eleven"); mapping.insert(42, "Forty-two");QVariant变量=QVariant::fromValue(mapping);if(variant.canConvert<QVariantHash>()) {QAssociativeIterableiterable=variant.value<QAssociativeIterable>();// 可以对值使用 foreach:foreach(constQVariant&v,iterable) { qDebug() << v; }// 可以在值上使用 C++11 range-for: for(constQVariant&v: iterable) { qDebug() << v; }// 可以使用迭代器: QAssociativeIterable::const_iterator it=iterable.begin();const QAssociativeIterable::const_iterator end=iterable.end();for( ; it!=end;++it) { qDebug() << *it; // The current value qDebug() << it.key(); qDebug() << it.value(); } }
在遍历容器之前,容器本身不会被复制。
另请参见 QVariant 。
成员类型文档
[alias]
QAssociativeIterable::BidirectionalConstIterator
使用 std::bidirectional_iterator_tag 公开一个const_iterator 。
[alias]
QAssociativeIterable::BidirectionalIterator
使用 std::bidirectional_iterator_tag 公开一个迭代器。
[alias]
QAssociativeIterable::ForwardConstIterator
使用 std::forward_iterator_tag 公开一个const_iterator 。
[alias]
QAssociativeIterable::ForwardIterator
使用 std::forward_iterator_tag 公开一个迭代器。
[alias]
QAssociativeIterable::InputConstIterator
使用 std::input_iterator_tag 公开一个const_iterator 。
[alias]
QAssociativeIterable::InputIterator
使用 std::input_iterator_tag 公开一个迭代器。
[alias]
QAssociativeIterable::RandomAccessConstIterator
使用 std::random_access_iterator_tag 公开一个const_iterator 。
[alias]
QAssociativeIterable::RandomAccessIterator
使用 std::random_access_iterator_tag 公开一个迭代器。
[alias]
QAssociativeIterable::const_iterator
QAssociativeIterable::const_iterator 允许对QVariant 中的容器进行迭代。
QAssociativeIterable::const_iterator 只能由QAssociativeIterable 实例创建,其使用方式与其他 stl 风格迭代器类似。
QHash<int、 QString>mapping; mapping.insert(7, "Seven"); mapping.insert(11, "Eleven"); mapping.insert(42, "Forty-two");QVariant变量=QVariant::fromValue(mapping);if(variant.canConvert<QVariantHash>()) {QAssociativeIterableiterable=variant.value<QAssociativeIterable>();// 可以对值使用 foreach:foreach(constQVariant&v,iterable) { qDebug() << v; }// 可以在值上使用 C++11 range-for: for(constQVariant&v: iterable) { qDebug() << v; }// 可以使用迭代器: QAssociativeIterable::const_iterator it=iterable.begin();const QAssociativeIterable::const_iterator end=iterable.end();for( ; it!=end;++it) { qDebug() << *it; // The current value qDebug() << it.key(); qDebug() << it.value(); } }
另请参见 QAssociativeIterable 。
[alias, since 6.0]
QAssociativeIterable::iterator
QAssociativeIterable::iterator 允许对QVariant 中的容器进行迭代。
QAssociativeIterable::iterator 只能由QAssociativeIterable 实例创建,其使用方式类似于其他 stl 样式的迭代器。
该类型定义在 Qt 6.0 中引入。
另请参阅 QAssociativeIterable 。
成员函数文档
bool QAssociativeIterable::containsKey(const QVariant &key)
如果容器中有包含给定key 的条目,则返回true
,否则返回false
。如果key 不能转换为预期类型,则返回false
。
QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant &key) const
检索指向给定key 的元素的const_iterator ,如果该键不存在,则检索容器的末端。如果key 不能转换为预期类型,则返回容器的末端。
void QAssociativeIterable::insertKey(const QVariant &key)
用给定的key 插入一个新条目,或用给定的key 将任何现有条目的映射值重置为默认构造的映射值。key 被强制转换为预期类型:如果不可转换,则插入默认值。
QAssociativeIterable::iterator QAssociativeIterable::mutableFind(const QVariant &key)
检索指向给定key 的元素的迭代器,如果该键不存在,则检索容器的末端。如果key 不能转换为预期类型,则返回容器的末端。
void QAssociativeIterable::removeKey(const QVariant &key)
从容器中删除带有给定key 的条目。key 被强制转换为预期类型:如果不可转换,则删除默认值。
void QAssociativeIterable::setValue(const QVariant &key, const QVariant &mapped)
如果可能,将与key 相关联的映射值设置为mapped 。如果给定的key 不存在,则插入一个新条目。如果key 不能转换为键类型,则默认键类型的值将被覆盖。
另请参见 value().
QVariant QAssociativeIterable::value(const QVariant &key) const
读取给定key 的映射值,如果键不存在,则读取映射类型的默认构造QVariant 。如果key 不能转换为键类型,则返回与默认构造键相关的映射值。
另请参阅 setValue() 。
© 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.