Results 1 to 6 of 6

Thread: QHash with 2 keys?

  1. #1
    Join Date
    Mar 2007
    Posts
    74
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QHash with 2 keys?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHash with 2 keys?

    How about using QVariantMap? or a QMap/QHash of QVariantMaps.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash with 2 keys?

    Quote Originally Posted by MrGarbage View Post
    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.

  4. #4
    Join Date
    Mar 2007
    Posts
    74
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHash with 2 keys?

    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<?,?>

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHash with 2 keys?

    Quote Originally Posted by MrGarbage View Post
    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 > >

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHash with 2 keys?

    Just make sure there is a whitespace between those ending ">" characters. Current C++ standard needs that.

Similar Threads

  1. signals and slots with container types like QHash
    By themolecule in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2007, 08:07
  2. QHash compile error
    By jiveaxe in forum Qt Programming
    Replies: 10
    Last Post: 27th August 2007, 13:58
  3. Query about QHash , QList
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2006, 09:04
  4. Handling of dead keys in keyPressEvent()
    By ghorwin in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2006, 12:26
  5. about QHash elements order
    By bruce1007 in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2006, 07:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.