Important: Please read the Qt Code of Conduct -

How to pack the c++ data into QVariant Object list

  • In C++ I am having list of int,QStringList value for the object. I want to pack it in the QVariant Object list and access into the QML.

    I tried to access the qml i am not able to get the object properties.

    How can I unpack the data from the QVariant having QVariant list of QObject from c++ to QML. Its very helpful any one of you help me,how i can proceed.

    Thanks In Advance

  • Can any one of you help me for the above question

  • I am having the the list of items like QStringList, QString,int in c++ class. How I can pack all the data into object list and send to qml.

    I tried but i am not getting the values in QML.
    How i can send the data from c++ to qml and access all the element in the list in QML.

    Can any one of you give idea its very helpful.

    Thanks in advance

  • Hi,

    You can use code like this:
    QVariant getVaraintObect() //This method must have prototype with Q_INVOKABLE macros in you C++ class.
    QVariantMap map;
    map["key1"] = list; //your QVariantList object with items.
    map["key2"] = 20; //your int value.
    return QVariant(map); //The method return QVariant object.

    In QML you call the method of your class which must to return to you your QVaraint object, for example:
    Text {
    text: yourCPPObject.getVaraintObect().key2.

    In result you must see 20 in Text element of your QML file.
    Also you can use console.log to print all structure of object:

    In result you must must see something like this:
    {"key1": [], "key2": 20}

Log in to reply