Questions tagged [qmap]

QMap is a Qt container class that implements a map a.k.a. a skip-list-based dictionary.

This container looks like this:

QMap< Key, T >

It stores key-value pairs, and provides access to the value by the key and vice-versa. It also provides iterator-based access. A typical usage of the QMap is as follows:

//constructs a map with strings as keys and integers as values.
QMap< QString, int > myMap;
//this will set the value for the key "qwerty" if it doesn't exist, or override the current value if it does exist:
myMap["qwerty"] = 15;
//accessing elements is as easy as this:
int i = myMap["qwerty"];

The official QMap documentation can be found here for Qt 4.8 and here for Qt 5.

189 questions
70
votes
10 answers

Iterating over a QMap with for

I've a QMap object and I am trying to write its content to a file. QMap extensions; //.. for(auto e : extensions) { fout << e.first << "," << e.second << '\n'; } Why do I get: error: 'class QString' has no member named…
user336635
  • 2,081
  • 6
  • 24
  • 30
14
votes
2 answers

QMap::contains() VS QMap::find()

I often see code like: if(myQMap.contains("my key")){ myValue = myQMap["my key"]; } which theoretically performs two look-up's in the QMap. My first reaction is that it should be replaced by the following, which performs one lookup only and…
nbilal
  • 1,069
  • 2
  • 10
  • 21
13
votes
2 answers

Deleting all values from a QMap

I have a QMap consist of pointers to class objects, allocated using new. I need to delete all these pointers. What is the proper way of doing this with QMap ? I can do it this way: QList allVals = map.values(); for…
Littlebitter
  • 671
  • 3
  • 10
  • 19
11
votes
5 answers

Assigning to nested QVariantMap

#include #include #include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QVariantMap map; map["foo"] = QVariant(QVariantMap()); map["baz"] = "asdf"; …
Jeff
  • 148
  • 1
  • 1
  • 7
10
votes
3 answers

Does QMap support custom comparator functions?

I couldn't find a way to set a custom comparator function for QMap, like I can for std::map (the typename _Compare = std::less<_Key> part of its template arguments). Does QMap have a way to set one?
sashoalm
  • 75,001
  • 122
  • 434
  • 781
9
votes
1 answer

How to deep copy QMap and other Qt containers

Generally speaking, what is the correct way to deep copy Qt containers? I'm not worried about deep copying the containers recursively, although addressing such would be helpful.
Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
7
votes
1 answer

How to expose QMap to QML using QDeclarative

How can I expose QMap from C++ to QML? Using QList I can use QDeclarativeListProperty.
user1185305
  • 875
  • 2
  • 15
  • 26
7
votes
1 answer

why does qmap uses skiplist instead ob rb-tree?

I wounder why does QMap realised over skiplist data-structure and not rb-tree? There is very interesting SO thread about concurrency data-structs and skip-list benefits over rb-tree, pros and cons. It is indeed VERY interesing dialog with helpfull…
sohel
  • 377
  • 3
  • 13
6
votes
2 answers

QMap/QHash operator[] returned reference validity

I was wondering for how long the reference to a value inside a Qt container, especially a QHash or a QMap is valid. By valid I mean if it is guaranteed to still point to the correct location inside the map/hash after inserting or removing other…
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
6
votes
2 answers

QMap::insertMulti or QMultiMap?

What should i use between QMap::insertMulti and QMultiMap to handle : 2 -> abc 2 -> def 3 -> ghi 3 -> jkl What's the difference enter the 2 solutions ?
canardman
  • 3,103
  • 6
  • 26
  • 28
6
votes
2 answers

Order of items in QMap and QMultiMap

I would like to use QMultiMap (which is derived from QMap) to store key/value pairs. Since I can have keys multiple times I would prefer to use QMultiMap. Assume I would insert the following pairs in the given order: "C" -> 5 "A" -> 10 "B" -> 77 "B"…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
6
votes
3 answers

How to find specific value in Qmap

I need to know in QMap second value there is. Functions like first() and last() are useless. Do i have to use iterator, some kind of loop? QMap map; map.insert(1, "Mario"); map.insert(2, "Ples"); map.insert(3, "student"); …
mario
  • 111
  • 2
  • 3
  • 8
5
votes
1 answer

Changing and removing values from deeply nested QVariant

I'm using QVariant to manage the project settings of our In-House application. For this I'm using a nested QVariantMap recursively containing QVariantMaps and leaves holding the actual values. Now, I found it quite cumbersome to set and remove nodes…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
5
votes
1 answer

Qt: Questions about QMap thread-safety

I am using Qt5 on Windows 7. In my current app, I have the following (simplified here): QMap map; int _WorkerThread_(int index) { QString new_element = "whatever"; ... map.insert(index, new_element); // [Q1] …
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
5
votes
2 answers

How do I convert QMap > to a QVariant?

QVariant (needed for QSettings class) supports creation from QMap But trying to initialise something like this: QMap)> i; Gives the error: function returning a function. So then I tried…
fenix
  • 215
  • 1
  • 3
  • 8
1
2 3
12 13