Search code examples
qtqt5qmapqhash

Is there a map-like tool in QT that can be iterated over inserted index?


From the Qt documentation about QMap::iterator :

Unlike QHash, which stores its items in an arbitrary order, QMap stores its items ordered by key. Items that share the same key (because they were inserted using QMap::insertMulti(), or due to a unite()) will appear consecutively, from the most recently to the least recently inserted value.

What I want is to interate a map by inserted index. For example this map.

const static QMap<QString, int> MEASUREMENT_COLUMNS{{"ID", MY_SQL_BIGINT}, {"logging_id", MY_SQL_INT}, {"calibration_id", MY_SQL_INT}, {"logging_comment", MY_SQL_VARCHAR255}, {"measurement_date_time", MY_SQL_DATETIME}, {"ADC0", MY_SQL_FLOAT},
                                                    {"ADC0", MY_SQL_FLOAT},
                                                    {"ADC1", MY_SQL_FLOAT},
                                                    {"ADC2", MY_SQL_FLOAT},

But the problem is as the documentation says above about QMap and QHashmap. They will not work for be if I want to iterate a map by inserted index.

For example, first ID, then logging_id, then calibration_id etc. So I need to select something else than QMap and QHash.

Question:

Is there a map-like tool in QT that can be iterated over inserted index?


Solution

  • You can use two QVector, or use QVector<QPair<QString, int> > instead.