Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QList of QPair in QML
Qt 6.11 is out! See what's new in the release blog

QList of QPair in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sillimon
    wrote on last edited by Sillimon
    #1

    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 !

    raven-worxR 1 Reply Last reply
    0
    • S Sillimon

      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 !

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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"
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      S 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @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"
        
        S Offline
        S Offline
        Sillimon
        wrote on last edited by
        #3

        @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 !

        raven-worxR 1 Reply Last reply
        0
        • S Sillimon

          @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 !

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sillimon
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved