Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Nested QMap with a QHash and fetching values inside the QHash?



  • Howdy folks, hope your 2022 is going well so far. Another day trying to train myself on C++ and QT. I've been playing around with containers and noticed something odd with using QMaps. I can only fetch the outer most keys and never anything inside that.
    For example, say I have this setup of a customer ID dataset:

    QMap(("customerIdData", QVariant(QVariantHash, QHash(("Mike", QVariant(double, 0439.7))))))
    

    Using the QMap.value("customerIdData") works well but this QMap.value("Mike") only shows the default 0 value. Is this possible to access that inner QHash double value?

    Appreciate the help as always.


  • Lifetime Qt Champion

    Please provide some real code on how you fill and later try to read your structure.


  • Lifetime Qt Champion

    Hi,

    Beside the code, you shall start by doing it in steps:

    • Get the variant from the map
    • Convert the variant to a hash
    • Extract the value you want from the hash


  • @SGaist said in Nested QMap with a QHash and fetching values inside the QHash?:

    Hi,

    Beside the code, you shall start by doing it in steps:

    • Get the variant from the map
    • Convert the variant to a hash
    • Extract the value you want from the hash

    Perfect, thanks guys! This worked

    QMap.first().toHash().value("Mike").toDouble()
    

Log in to reply