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



  • 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 )?



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



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



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



  • 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)



  • 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)@



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



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


Log in to reply
 

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