QVariantMap map; map.insert("name", "John"); map.insert("age", 30); for (auto it = map.constBegin(); it != map.constEnd(); ++it) { QString key = it.key(); QVariant value = it.value(); qDebug() << key << ":" << value; }
QVariantMap map; map.insert("name", "John"); map.insert("age", 30); QString key = "name"; if (map.contains(key) && map.find(key) != map.constEnd()) { QVariant value = map.value(key); qDebug() << key << ":" << value; }In this example, a QVariantMap is created with two key-value pairs. The contains() method is used to check if the map contains a specific key. If the key exists, find() method is used to get the iterator pointing to the item with the key in the QMap. The iterator is then compared with the constEnd() method to make sure the key exists in the QMap. If the key exists, the value is printed out using qDebug().