[英]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
不是有效的密鑰時,根據您想要的行為,您的代碼將有所不同。
如果要忽略該鍵(如果map
沒有該鍵),則可以使用:
if ( map.find(rand) != map.end() ) { myMap.insert(map.key(rand), map.value(rand)); }
如果要在鍵不存在的情況下使用默認構造的值映射,則不會根據上述代碼創建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.