Qt5 Tutorial QHash - 2020
In this tutorial, we will learn about QHash.
The QHash class is a template class that provides a hash-table-based dictionary.QHash<Key, T> is one of Qt's generic container classes. It stores (key, value) pairs and provides very fast lookup of the value associated with a key.
QHash provides very similar functionality to QMap. The differences are:
- QHash provides faster lookups than QMap.
- When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
- The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash().
Key lookup | Insertion | |||
---|---|---|---|---|
Average | Worst case | Average | Worst case | |
QMap<Key, T> | O(log n) | O(log n) | O(log n) | O(log n) |
QMultiMap<Key, T> | O(log n) | O(log n) | O(log n) | O(log n) |
QHash<Key, T> | Amort. O(1) | O(n) | Amort. O(1) | O(n) |
QSet<Key> | Amort. O(1) | O(n) | Amort. O(1) | O(n) |
#include <QCoreApplication> #include <QHash> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QHash<int, QString> Hash; Hash.insert(1,"A"); Hash.insert(2,"B"); Hash[3] = "C"; foreach(int i, Hash.keys()) qDebug() << Hash[i]; QHashIterator<int, QString> iter(Hash); while(iter.hasNext()) { iter.next(); qDebug() << iter.key() << " : " << iter.value(); } return a.exec(); }
Output should look like this:
"A" "B" "C" 1 : "A" 2 : "B" 3 : "C"
To insert a (key, value) pair into the hash, we can use insert():
Hash.insert(1,"A"); Hash.insert(2,"B");
or use operator[]():
Hash[3] = "C";
We can store multiple values per key by using insertMulti() instead of insert().
If we want to navigate through all the (key, value) pairs stored in a QHash, we can use an iterator. QHash provides both Java-style iterators (QHashIterator and QMutableHashIterator) and STL-style iterators (QHash::const_iterator and QHash::iterator). The example above shows how to iterate over a QHash<QString, int> using a Java-style iterator:
QHashIterator<int, QString> iter(Hash); while(iter.hasNext()) { iter.next(); qDebug() << iter.key() << " : " << iter.value(); }
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization