| ID | ... | Creation date | 12 January 2009 |
| Platform | S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition | Tested on devices | Emulator |
| Category | Qt for Symbian | Subcategory | Application |
| Keywords (APIs, classes, methods, functions): QHash,QMap |
This code snippet demonstrates how to create a Dictionary in Qt for S60. The QHash class is a template class that provides a hash-table-based dictionary. It stores (key, value) pairs and provides very fast lookup of the value associated with a key.
QHash provides very similar functionality to QMap. Both are used to create a dictionary.
QHash<QString, int> hash;
int num1 = hash["thirteen"];
int num2 = hash.value("thirteen");
hash.insert("plenty", 100);
hash.insert("plenty", 2000);
// hash.value("plenty") == 2000
#include <QHash>
#include <QLabel>
#include <QtGui>
#include <QApplication>
#include <QString>
#include <QHBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QHash<QString, int> hash;
QWidget win;
QHBoxLayout *layout = new QHBoxLayout;
hash["one"] = 1;
hash["three"] = 3;
hash["seven"] = 7;
hash.insert("twelve", 12);
QLabel *label1=new QLabel;
int num2=2;
int num1 = hash.value("one");
if (hash.contains("three")) // Checks in the hash table, Return the value if it exist
num2 = hash.value("three");
int num3 = hash["seven"];
int x = hash.value("thirty", 30); // If "thirty" is not there in a hash table than assign 30 to x.
QString str1;
str1.setNum(num1);
label1->setText(str1);
layout->addWidget(label1);
win.setLayout(layout);
win.show();
return a.exec();
}
QHash provides very similar functionality to QMap. The differences are:
#include "Qdict.h"
#include <QtGui>
#include <QApplication>
#include <QHash>
#include<QMap>
#include <QLabel>
#include <QString>
#include <QHBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMap<QString, int> map;
QWidget win;
QHBoxLayout *layout = new QHBoxLayout;
map["one"] = 1;
map["three"] = 3;
map["seven"] = 7;
map.insert("twelve", 12);
QLabel *label1=new QLabel;
int num2=2;
int num1 = map.value("one");
if (map.contains("three")) // Checks in the hash table, Return the value if it exist
num2 = map.value("three");
int num3 = map["seven"];
int x = map.value("thirty", 30); // If "thirty" is not there in a hash table than assign 30 to x.
QString str1;
str1.setNum(num1);
label1->setText(str1);
layout->addWidget(label1);
win.setLayout(layout);
win.show();
return a.exec();
}
No related wiki articles found