繁体   English   中英

如何将一个QMap放入另一个QMap

[英]How to put one QMap into another QMap

项目应给出一个随机数,但并不重要,然后在第一张地图中找到该随机数并添加到第二张地图中。

int rand = 2;
QPixmap pixmap1 = QPixmap (":/imag/sedam_one.jpg");
QPixmap pixmap2 = QPixmap (":/imag/gedam_one.jpg");
QPixmap pixmap3 = QPixmap (":/imag/tedam_one.jpg");
QMap<int, QPixmap> map;
map.insert(1, pixmap1);
map.insert(2, pixmap2);
map.insert(3, pixmap3);
QMap<int, QPixmap> myMap;
myMap.insert(map.key(rand), map.value(rand));

rand不是有效的密钥时,根据您想要的行为,您的代码将有所不同。

  1. 如果要忽略该键(如果map没有该键),则可以使用:

     if ( map.find(rand) != map.end() ) { myMap.insert(map.key(rand), map.value(rand)); } 
  2. 如果要在键不存在的情况下使用默认构造的值映射,则不会根据上述代码创建if检查,而只使用您拥有的代码:

     myMap.insert(map.key(rand), map.value(rand)); 

这是一种方法:

  int rand = 2;
 QPixmap pixmap1 = QPixmap (":/imag/sedam_one.jpg");
 QPixmap pixmap2 = QPixmap (":/imag/gedam_one.jpg");
 QPixmap pixmap3 = QPixmap (":/imag/tedam_one.jpg");
 QMap<int, QPixmap> map;
 map.insert(1, pixmap1);
 map.insert(2, pixmap2);
 map.insert(3, pixmap3);
 QMap<int, QPixmap> myMap;
 myMap.insert(rand, map.value(rand));

注意最后一行。 map.key(rand)应该只是rand因为map.key()方法要求您输入一个值,例如QPixmap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM