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. How to address all the ListModel ListElements and not just one?
Qt 6.11 is out! See what's new in the release blog

How to address all the ListModel ListElements and not just one?

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 14.2k 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.
  • C Offline
    C Offline
    cmer4
    wrote on last edited by
    #1

    Hi gurus,
    Another one riddle (more like a clarification needed)
    I have the following vendorList:

    @ListModel {
    id: vendorsModel
    ListElement {
    vendortitle: "Vendor"
    vendorlogo: "images/vendorlogo.png"
    openhours: "10am-5am"
    favorite: 0
    averagedeliverytime: "1h"
    additionalinfo: "this is some additional information here"
    }
    ListElement {
    vendortitle: "Vendor1"
    vendorlogo: "images/vendor2logo.png"
    openhours: "10am-5am"
    favorite: 0
    averagedeliverytime: "1h"
    additionalinfo: "this is some additional information here"
    }@

    There is a property "favorite" that can change to 1 if user toggles the button.
    I want to implement a button that resets all the favorites in all the ListElements to 0 (like Reset All button).

    Is there a technique to do that?
    or I need to address each and every ListElement via something like: ListModel::setProperty ( int index, string property, variant value )?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      There is no such method to do it in ListModel, but iterating through elements and use setProperty for them is not a something hard. Simple loop from 0 to count with one operation.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I assume that at one point, you will want to start using a real model that supports persistence instead of the fixed model you use now, right? In that case, you could simply use a custom model in C++ that you use from QML. And that custom model could simply have a resetFavorites() slots you can call from your QML.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cmer4
          wrote on last edited by
          #4

          Hmm...Andre, you probably right, but I am a real rookie in C++ yet and for now I need to complete the demo based on QML and maybe javascript that I have some understanding...

          Denis, you mentioned simple loop? what does it mean? do you mean to do it in JS? or is there QML based method to do it? any suggestion is very welcomed!

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vinb
            wrote on last edited by
            #5

            You have "while" and "for" loops in almost every program language you can think of. I like the foreach loop wich Qt provides. (its not a standard c++ loop)

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cmer4
              wrote on last edited by
              #6

              Hi folks, sorry but I am quite a noob in all of that hence some things are harder to understand (yet). But your help is supr appreciated...

              So basically what I achieved so far is to insert this function below into QML element:

              @Text {
              id: favoritesTab
              function totalFavorites() {
              for (var i=0; i < vendorsModel.count; i++)
              return vendorsModel.get(i).favorite
              }
              text: "Favorites:" + totalFavorites()@

              As seen from the JS code I am only getting returned value for the first ListElement...I am now trying to implement somthing that will just sum-up the returned looped items...

              How can I add ssomething like: @return SUM(vendorsModel.get(i).favorite)@

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DenisKormalev
                wrote on last edited by
                #7

                @
                function totalFavorites() {
                var result = 0
                for (var i=0; i < vendorsModel.count; i++)
                result += vendorsModel.get(i).favorite
                return result
                }
                @

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cmer4
                  wrote on last edited by
                  #8

                  Ah I see...thanks!;) that helps to address similar issues as well...but I definitely need an upgrade...(of my expertise...)

                  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