Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-99145

Add 'filter' method to QML list type

XMLWordPrintable

      JavaScript arrays have a filter method that allows one to create a new list that includes selective elements from the source list.

      It would be nice to have a filter method on the QML list type, which would do the same thing, only operating on QML lists instead of JavaScript arrays.

      Suggested syntax would be exactly the same as the JavaScript Array.prototype.filter method.

      An example that I was expecting to work is below and attached:

      import QtQuick
      import QtQuick.Controls
      import QtQuick.Layouts
      
      ApplicationWindow {
          width: 640
          height: 480
          visible: true
      
          ColumnLayout {
              anchors.fill: parent
      
              Component.onCompleted: {
                  var words = [ "Apple", "Pear", "Orange", "Banana", "Melon", "Dragonfruit" ];
                  function eFilterArray(currentElement) {
                      return currentElement.includes("e");
                  }
                  console.log("Array " + words + " length: " + words.filter(eFilterArray));
                  function eFilterText(currentElement) {
                      return currentElement.text.includes("e");
                  }
                  var eChildren = children.filter(eFilterText, 0);
                  console.log("Number of fields that contain 'e' in their text: " + eChildren);
              }
      
              Text { text: "Apple" }
              Text { text: "Pear" }
              Text { text: "Orange" }
              Text { text: "Banana" }
              Text { text: "Melon" }
              Text { text: "Dragonfruit" }
          }
      }
      

        1. listfilter.qml
          1.0 kB
          Keith Kyzivat
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            qtqmlteam Qt Qml Team User
            kkyzivat Keith Kyzivat
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes