QList of QPair in QML



  • Hi !
    I have a listview to contain with sectors, each of those have a specific number of subparts. I'm using a QList<QPair<QString, int>> but i just realized you can't return it in QML.

    I was searching for something like this :

    for(var i = 0; i < Card.getSectorAndBlocks().length; i++)
                        {
                            var QList = Card.getSectorAndBlocks()
                            var QPairAti = QList[i]
                            listModel.append({sector: QPairAti.first, numSector: i, nbrModelRepeater: QPairAti.second})
                        }
    

    I then started to search for another solution and someone said : "you could use a QVariantList or QVariantMap which are 'returnable' in QML".

    The point is, i don't get what a QVariantList or QVariantMap are used for... What's the difference with other "usual" containers ?

    How can i mentally represent the way it "stocks" data and how it is different than a simple QStringList ?

    Thanks a lot for future answers !


  • Moderators

    @Sillimon said in QList of QPair in QML:

    The point is, i don't get what a QVariantList or QVariantMap are used for... What's the difference with other "usual" containers ?

    when you take a look at the docs you will notice that they are just typedefs for QList<QVariant> and QMap<QString,QVariant>

    So to "simulate" a list of QPairs return the following to QML:

    QVariantList list;
         QVariantMap pair1;
             pair1.insert("first", "foo");
             pair1.insert("second", "bar");
         list << pair1;
    // return list to QML
    

    in QML:

    list[0].first // = "foo"
    list[0].second // = "bar"
    


  • @raven-worx said in QList of QPair in QML:

    when you take a look at the docs you will notice that they are just typedefs for QList<QVariant> and QMap<QString,QVariant>

    So QVariantList is for QList<QVariant> what QStringList is to QList<String> ?

    The only difference i'd see is that QStringList inherits from QList but QVariantList is written as a simple synonym. There might be some differences between both... but anyway it works fine ! Thanks a lot !


  • Moderators

    @Sillimon said in QList of QPair in QML:

    The only difference i'd see is that QStringList inherits from QList but QVariantList is written as a simple synonym

    QVariantList a typedef.

    QStringList inherits from QList<QString> and adds just some convenience methods.



  • Fine ! Explanations clear and no more questions, i'll keep those in mind =)
    Thank you !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.