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. [SOLVED]Javascript used to remove specific ListElements (works but has one issue)
QtWS25 Last Chance

[SOLVED]Javascript used to remove specific ListElements (works but has one issue)

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

    I am continuing my learning curve and today tried to create a button click on which removes all the ListElements from the ListModel that have property "quantity" ==0. Here is the JS code I put:
    @ function removeZeroQuantity() {
    var i = 0
    for (var i=0; i < listModel.count; i++)
    if (foodsModel.get(i).quantity==0) { listModel.remove(i) }
    }@

    Well it works but seems like deletes not all zeros at once, with 6 total it removes 3, then 2, then 1...
    What am I doing wrong? how to make sure the function removes all of them from the first click??

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Aleskey78
      wrote on last edited by
      #2

      You check 'foodsModel' for 'quantity==0' and then remove an element from 'listModel' ???

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

        For this code:
        @function removeZeroQuantity() {
        var i = 0
        for (var i=0; i < listModel.count; i++)
        if (listModel.get(i).quantity==0) { listModel.remove(i) }
        }@
        Your index is no more valid after removing an element from the list.

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

          Trick when removing items from a container while iterating over that container: iterate backwards, from the end to the beginning. That way, your indices will stay valid.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            secretNinja
            wrote on last edited by
            #5

            @Andre Thank you for this trick! This made my day (regarding coding) :)

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

              heheh - indeed! Super!!

              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