PDA

View Full Version : QHash with 2 keys?



MrGarbage
5th September 2007, 20:24
Hi,

I am putting together a DOM parser for an XML file that
I will be using. There is a visible tree which
maps to the data in the XML file and that portion
is working.

I am struggling somewhat with how to store this data
internally. I need to be able to access and update it.

My data takes the form: (where there may be 50 device entries)

<device name="dev 01">
<attribute name="type" value="dev1" />
<attribute name="Address" value="1" />
<attribute name="Gain" value="1.275" />
<attribute name="Offset" value="8.275" />
<attribute name="Manufacturer" value="Widget company" />
</device>
<device name="dev 02">
<attribute name="type" value="dev1" />
<attribute name="Address" value="1" />
<attribute name="Gain" value="1.275" />
<attribute name="Offset" value="8.275" />
<attribute name="Manufacturer" value="Different Widget company" />
</device>

I was thinking of QHash, but I'm not sure that is sufficient and I need
some 50 QHash containers.

For a given device name, I want to be able to recover the "value" for
a given "name".

In addition I would like to be able to handle different types of
data, ie text, integer and float.

I could store my data as all QStrings, but somehow when I need
to perform calculations I need to be able to know whether data is
"int" or "float", etc.

Something like:

double m_offset = HashTableItem.value("dev 02", "Offset");

which would return the floating point "Offset" value for the "dev 02" entry.

integer m_address = HashTableItem.value("dev 01", "Address");

which would return the integer "Address" value for the "dev 01" entry.

Hope I'm not too far of course here..

Many thanks...
Mark

wysota
5th September 2007, 20:30
How about using QVariantMap? or a QMap/QHash of QVariantMaps.

jacek
5th September 2007, 21:31
For a given device name, I want to be able to recover the "value" for a given "name".
I would use a hash of stuctures, but you can also try a hash of hashes or a hash with pairs as keys.

MrGarbage
5th September 2007, 21:50
Yes, I think QVariant is the way to address my data type issue.
ie. QHash<QString, QVariant>

However Re: A hash of hashes or a hash with pairs as keys...
- what is the syntax to implement that?

ie. QHash<?,?>

jacek
5th September 2007, 22:45
A hash of hashes or a hash with pairs as keys...
- what is the syntax to implement that?
QHash< QPair< QString, QString >, QVariant >
QHash< QString, QHash< QString, QVariant > >

wysota
6th September 2007, 01:09
Just make sure there is a whitespace between those ending ">" characters. Current C++ standard needs that.