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. Looping over a QVariantList Q_PROPERTY in QML
Forum Updated to NodeBB v4.3 + New Features

Looping over a QVariantList Q_PROPERTY in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 1.9k Views
  • 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.
  • A Offline
    A Offline
    Ankit.Jain
    wrote on last edited by
    #1

    Hi,

    I had exposed a QVariantList (can be converted into a QStringList) as a Q_PROPERTY to my QML code. I was using it to populate a combo box (by setting it as the model). Now, this recently someone created a function in QML which I am required to call over every object in this particular list before setting it into the combo box.

    Basically, I need to call list2[listIndex] = func(list[listIndex]) over every element in the list and save the new values to a separate list to set as model.

    I tried writing a for loop to loop over the list, but I realized that I have no way to know it's length. listName.length returns 0. I also don't want to create a property when defining a list, so I am stuck with the inability to append the output of func to list2 as well.

    Any help would be appreciated,
    Thanks
    Ankit

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by GrecKo
      #2

      length is defined for a QVariantList, I tested with a context property and also via Q_PROPERTY.

      But you don't need it, you can just use Array.map():

      model: list1.map(n => n * 2)
      

      EDIT: And if your are doing that because you now have a list of objects instead of a list of strings, make sure to check the textRole property of ComboBox, it might help you.

      A 1 Reply Last reply
      2
      • GrecKoG GrecKo

        length is defined for a QVariantList, I tested with a context property and also via Q_PROPERTY.

        But you don't need it, you can just use Array.map():

        model: list1.map(n => n * 2)
        

        EDIT: And if your are doing that because you now have a list of objects instead of a list of strings, make sure to check the textRole property of ComboBox, it might help you.

        A Offline
        A Offline
        Ankit.Jain
        wrote on last edited by Ankit.Jain
        #3

        @GrecKo Thanks for check on the length variable. Maybe I had some other bug originally when I tested, but that part's working.
        The Array.map() function on the other hand isn't. I wrote something similar to what you had defined

        contextName.listName.map(x => context2Name.obj.func(x))
        

        and the code gave me a syntax error on the "=>" symbol. I don't think that QVariantList is treated as a list type. I tried setting it to a list property to see it fail.

        Anyway, I am thinking of trying QQmlListProperty once.

        P.S. The QVariantList is a list of strings and I am using Qt v5.6.2

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          x => context2Name.obj.func(x) is a shorthand for function (x) { return context2Name.obj.func(x); } but it is only available since Qt 5.12 with the support of EcmaScript 6 and later.

          In your case you can just do:

          contextName.listName.map(context2Name.obj.func)
          

          since map's parameter is a function that it applies on each element of your array.

          Don't use QQmlListProperty, it's for declaring a list property that is meant to be populated from QML.

          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