PDA

View Full Version : QMap, QHash and QByteArray



ugluk
7th July 2011, 16:19
I'd like to do:

QHash<QByteArray, some_type> and
QMap<QByteArray, some_type>

but can't do either, as there is no hashing function defined for QHash and no comparison function defined for QMap. I'm almost certain there exist quick fixes to this situation, just as they do with STL, please share them with me.

mcosta
7th July 2011, 16:31
What Qt version you're using?

This code



#ifndef WIDGET1_H
#define WIDGET1_H

#include <QtGui/QWidget>

#include <QMap>
#include <QHash>

namespace Ui {
class Widget1;
}

class Widget1 : public QWidget
{
Q_OBJECT

public:
explicit Widget1(QWidget *parent = 0);
~Widget1();

private:
Ui::Widget1 *ui;

QMap <QByteArray, int> myMap;
QHash <QByteArray, int> myHash;
};

#endif // WIDGET1_H


compile

stampede
7th July 2011, 16:54
This works too, with custom datatype:


#include <QtCore>

struct s_{
QString str;
int val;
};

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

s_ s; s.str = "hello"; s.val = 10;
QMap<QByteArray,s_> map;
QHash<QByteArray,s_> hash;
map["string"] = s;
hash["other key"] = s;

qDebug() << map["string"].str << map["string"].val;
qDebug() << hash["other key"].str << hash["other key"].val;

return 0;
}

What errors do you get ?

ugluk
7th July 2011, 17:11
I've upgraded to a new version and everything seems to work now, thank you for helping. I used to use 4.6.2.