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

How to Seperate QVariantMap values into individual elements



  • Hi All,

    I have a QVariantMap map =
    (QVariant(QVariantList, (QVariant(QString, "1"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "6"), QVariant(QString, "12"), QVariant(QString, "4"))), QVariant(QVariantList, (QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "6"), QVariant(QString, "14"))), QVariant(QVariantList, (QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "8"), QVariant(QString, "14"))), QVariant(QVariantList, (QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "9"), QVariant(QString, "16"))), QVariant(QVariantList, (QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "10"), QVariant(QString, "16"))), QVariant(QVariantList, (QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "11"), QVariant(QString, "18"))), QVariant(QVariantList, (QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "12"), QVariant(QString, "20"))), QVariant(QVariantList, (QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "14"), QVariant(QString, "24"))))

    How can I seperate the above data into QVariantList and to individual values.


  • Lifetime Qt Champion

    @Praveen-Illa From documentation:

    typedef QVariantMap
    
    Synonym for QMap<QString, QVariant>.
    

    If you want to know how to use QMap: https://doc.qt.io/qt-5/qmap.html



  • @jsulm - Thanks for Quick response.
    I have used QMapIterator as below
    QMapIterator<QString, QVariant> i(map);
    while (i.hasNext())
    {
    i.next();
    qDebug() << i.key() << ": " << i.value();
    }
    The output is Key : Value
    "Period 1" : QVariant(QVariantList, (QVariant(QString, "1"), QVariant(QString, "1"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "0"), QVariant(QString, "1"), QVariant(QString, "6"), QVariant(QString, "12"), QVariant(QString, "4")))

    Now, again I need to seperate the value part to individual QStrings. How can I do that ??


  • Lifetime Qt Champion

    This post is deleted!

  • Lifetime Qt Champion

    @Praveen-Illa So, your value is QVariantList? Then you can simply iterate over the list as usual.

    typedef QVariantList
    
    Synonym for QList<QVariant>.
    


  • @jsulm
    I have tried like below, but getting error " error: error: conversion from ‘const QVariant’ to non-scalar type ‘QVariantList {aka QList<QVariant>}’ requested"

    QMapIterator<QString, QVariant> i(map);
    while (i.hasNext())
    {
    i.next();
    qDebug() << i.key() << ": " << i.value();
    QVariantList list = i.value();
    }

    Could you please suggest ?



  • @Praveen-Illa said in How to Seperate QVariantMap values into individual elements:

    QVariantList list = i.value();

    your map is an instance of QVariantMap which is a typedef of QMap<QString, QVariant>
    This means i.value() is a QVariant.

    Now you know that is variant holds a QVariantList (which is a typedef of QList<QVariant>):

    if (i.value().canConvert<QVariantList>()) {
        QSequentialIterable iterable = i.value().value<QSequentialIterable>();
         foreach (const QVariant &v, iterable) {
             qDebug() << v;
         }
    }
    

    ==> Read documentation for more details: =>https://doc.qt.io/qt-5/qvariant.html#canConvert

    i.value() will returns what it is ==> a QVariant.
    To get the holded value you have to "cast" ==> i.value().value<QVariantList>();



  • @KroMignon and @jsulm - Thank you both for your inputs and it was working now.


Log in to reply