【Qt之QAssociativeIterable】使用

介绍

QAssociativeIterable类是QVariant中一个关联式容器的可迭代接口。这个类允许多种访问在QVariant中保存的关联式容器元素的方法。如果一个QVariant可以转换为QVariantHashQVariantMap,那么QAssociativeIterable的实例可以从中提取出来。

cpp 复制代码
QHash<int, QString> mapping;
  mapping.insert(7, "Seven");
  mapping.insert(11, "Eleven");
  mapping.insert(42, "Forty-two");

  QVariant variant = QVariant::fromValue(mapping);
  if (variant.canConvert<QVariantHash>()) {
      QAssociativeIterable iterable = variant.value<QAssociativeIterable>();
      // Can use foreach over the values:
      foreach (const QVariant &v, iterable) {
          qDebug() << v;
      }
      // Can use C++11 range-for over the values:
      for (const QVariant &v : iterable) {
          qDebug() << v;
      }
      // Can use iterators:
      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();
      }
  }

结果:

相关推荐
QX_hao3 小时前
【Go】--log模块的使用
开发语言·后端·golang
爱编程的鱼3 小时前
ESLint 是什么?
开发语言·网络·人工智能·网络协议
小陈不好吃3 小时前
Spring Boot配置文件加载顺序详解(含Nacos配置中心机制)
java·开发语言·后端·spring
Dan.Qiao3 小时前
python读文件readline和readlines区别和惰性读
开发语言·python·惰性读文件
渡我白衣3 小时前
链接的迷雾:odr、弱符号与静态库的三国杀
android·java·开发语言·c++·人工智能·深度学习·神经网络
A.A呐3 小时前
【QT第三章】常用控件1
开发语言·c++·笔记·qt
Bony-3 小时前
Go语言并发编程完全指南-进阶版
开发语言·后端·golang
007php0073 小时前
大厂深度面试相关文章:深入探讨底层原理与高性能优化
java·开发语言·git·python·面试·职场和发展·性能优化
say_fall4 小时前
C语言容易忽略的小知识点(1)
c语言·开发语言
不会编程的小寒4 小时前
C++初始继承,继承中构造、析构顺序
开发语言·python