SortFilterModel QML Type

SortFilterModel sorts and filters rows from an existing model. More...

Import Statement: import Lomiri.Components 1.3

Properties

Detailed Description

The SortFilterModel takes an existing model such as a ListModel or any QAbstractItemModel implementation. The original rows and role names show up in the SortFilterModel with two basic differences. For one if sort.property is set all rows will be sorted. Further more if filter.property is set only rows matching the filter will be in the model.

Example usage:

import QtQuick 2.9
import Lomiri.Components 1.3

MainView {
    width: units.gu(80)
    height: units.gu(40)

    ListModel {
        id: movies
        ListElement {
            title: "Esign"
            producer: "Chris Larkee"
        }
        ListElement {
            title: "Elephants Dream"
            producer: "blender"
        }
        ListElement {
            title: "Big Buck Bunny"
            producer: "Blender"
        }
    }

    SortFilterModel {
        id: sortedMovies
        model: movies
        sort {
            property: "title"
            order: Qt.DescendingOrder
        }
        filter {
            property: "producer"
            //Add i for case insensitive
            pattern: /Blender/i
        }
    }

    ListView {
        model: sortedMovies
        anchors.fill: parent
        delegate: ListItemLayout {
            title.text: model.title
            subtitle.text: model.producer
        }

        section {
            property: "title"
            criteria: ViewSection.FirstCharacter
            delegate: Text {
                text: i18n.tr(section)
            }
        }
    }
}

Pay attention to the differences between the original model and the result:

Property Documentation

filter.pattern : string

The pattern all rows must match, if filter.property is set.

Some examples:

  • /possible/ matches anywhere in a word, so both "impossible" and "possible".
  • /^sign/ matches "sign". But not "assignment" because ^ means start.
  • /vest$/ matches "safety vest" and "vest" but not "vested".
  • /bar/i matches "bar", "Bar" or "BAR" regardless of case.

For more advanced uses it's recommended to read up on Javascript regular expressions.


filter.property : string

If set to a valid role name, only rows matching filter.pattern will be in the model.


model : QAbstractItemModel

The source model to sort and/ or filter.


sort.order : string

The order, if sort.property is set. Qt::AscendingOrder sorts results from A to Z or 0 to 9. Qt::DescendingOrder sorts results from Z to A or 9 to 0.


sort.property : string

If set to a valid role name, all rows will be sorted according to sort.order.