Comparative Analysis of QMap and QHash in Qt

QMap Deep Dive

QMap is a sorted associative container storing key-value pairs in ascending key order.

  • The template class is defined as QMap<K, T>.
  • Elements are sorted based on keys.
  • Key type must overload operator<.

QMap Usage Example

#include <QtCore>

int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
    
    QMap<QString, int> sortedMap;
    sortedMap["Key B"] = 2;
    sortedMap["Key A"] = 0;
    sortedMap.insert("Key C", 1);

    // Iterate through keys (sorted order)
    foreach (const QString &key, sortedMap.keys()) {
        qDebug() << key;
    }

    // Iterate through values (sorted by key)
    foreach (int val, sortedMap.values()) {
        qDebug() << val;
    }

    // Using iterator
    QMapIterator<QString, int> it(sortedMap);
    while (it.hasNext()) {
        it.next();
        qDebug() << it.key() << ":" << it.value();
    }

    return app.exec();
}

Key Observations for QMap

  • Accessing non-existent keys returns default-constructed values.
  • Inserting existing keys overwrites the value.

QHash Deep Dive

QHash is a Qt hash table implementation for key-value storage.

  • Defined as QHash<K, T> template.
  • Elements are stored in arbitrary order.
  • Key type must overload operator==.
  • A global qHash() function must be defined for the key type.

QHash Usage Example

#include <QtCore>

int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
    
    QHash<QString, int> hashTable;
    hashTable["Key Z"] = 20;
    hashTable["Key X"] = 10;
    hashTable.insert("Key Y", 15);

    // Iterate keys (unsorted)
    foreach (const QString &key, hashTable.keys()) {
        qDebug() << key;
    }

    // Iterate values
    foreach (int val, hashTable) {
        qDebug() << val;
    }

    // Using const iterator
    QHash<QString, int>::const_iterator cit;
    for (cit = hashTable.cbegin(); cit != hashTable.cend(); ++cit) {
        qDebug() << cit.key() << ":" << cit.value();
    }

    return app.exec();
}

QMap vs QHash: Comparative Analysis

  • Identical APIs allow interchangeable usage.
  • Performance: QHash provides faster lookups.
  • Memory: QMap consumes less memory.
  • Ordering: QHash has arbitrary storage order; QMap maintains key-sorted order.
  • Key Requirements:
    • QHash requires operator== and qHash().
    • QMap requires operator<.

Practical Example: File Dialog Extension Handling

QString getFilteredFilePath(QFileDialog::AcceptMode mode, const QString &title) {
    QFileDialog dialog;
    QStringList filters;
    QMap<QString, QString> extMap;

    const char* filtersData[][2] = {
        {"Text (*.txt)", ".txt"},
        {"All Files (*.*)", "*"},
        {nullptr, nullptr}
    };

    for (int i = 0; filtersData[i][0] != nullptr; i++) {
        filters << filtersData[i][0];
        extMap.insert(filtersData[i][0], filtersData[i][1]);
    }

    dialog.setWindowTitle(title);
    dialog.setAcceptMode(mode);
    dialog.setNameFilters(filters);

    if (mode == QFileDialog::AcceptOpen) {
        dialog.setFileMode(QFileDialog::ExistingFile);
    }

    if (dialog.exec() == QFileDialog::Accepted) {
        QString path = dialog.selectedFiles().first();
        QString selectedExt = extMap[dialog.selectedNameFilter()];

        if (selectedExt != "*" && !path.endsWith(selectedExt)) {
            path += selectedExt;
        }
        return path;
    }
    return "";
}

Key Takeaways

  • Qt provides specialized containers for key-value storage.
  • QHash and QMap share similar interfaces.
  • QHash outperforms QMap in lookup speed.
  • QMap is more memory-efficient.
  • Key requiremetns differ between implementations.

Tags: Qt QMap QHash containers C++

Posted on Wed, 26 Aug 2026 16:52:43 +0000 by wrapper