A nicely exposed QSortFilterProxyModel for QML
Go to file
Grecko 2a439497ae doc: document the proxy role types 2017-09-26 22:28:07 +02:00
docs doc: Regenerate documentation for StringSorter 2017-09-12 01:29:47 +02:00
tests feat: add ExpressionRole proxy role type 2017-09-26 22:28:06 +02:00
.gitignore Add *.qmlc in .gitignore 2017-06-07 02:19:47 +02:00
LICENSE Initial commit 2016-02-24 03:01:22 +01:00
README.md Update README.md 2017-09-12 01:50:42 +02:00
SortFilterProxyModel.pri feature: Add new basic ProxyRole functionality 2017-09-16 16:44:56 +02:00
filter.cpp refactor: Refactor Sorter and Fitler to be consistent with ProxyRole 2017-09-17 21:01:53 +02:00
filter.h refactor: Refactor Sorter and Fitler to be consistent with ProxyRole 2017-09-17 21:01:53 +02:00
proxyrole.cpp doc: document the proxy role types 2017-09-26 22:28:07 +02:00
proxyrole.h feat: add ExpressionRole proxy role type 2017-09-26 22:28:06 +02:00
qpm.json Add qpm description 2016-02-24 03:28:13 +01:00
qqmlsortfilterproxymodel.cpp doc: document the proxy role types 2017-09-26 22:28:07 +02:00
qqmlsortfilterproxymodel.h fix: correctly emit dataChanged 2017-09-16 19:33:22 +02:00
sorter.cpp refactor: Refactor Sorter and Fitler to be consistent with ProxyRole 2017-09-17 21:01:53 +02:00
sorter.h refactor: Refactor Sorter and Fitler to be consistent with ProxyRole 2017-09-17 21:01:53 +02:00

README.md

SortFilterProxyModel

SortFilterProxyModel is an implementation of QSortFilterProxyModel conveniently exposed for QML.

Install

With qpm :
  1. qpm install fr.grecko.sortfilterproxymodel
  2. add include(vendor/vendor.pri) in your .pro if it is not already done
  3. import SortFilterProxyModel 0.2 to use this library in your QML files
Without qpm :
  1. clone or download this repository
  2. add include (<path/to/SortFilterProxyModel>/SortFilterProxyModel.pri) in your .pro
  3. import SortFilterProxyModel 0.2 to use this library in your QML files

Sample Usage

  • You can do simple filtering and sorting with SortFilterProxyModel:
import QtQuick 2.2
import QtQuick.Controls 1.2
import SortFilterProxyModel 0.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480

    ListModel {
        id: personModel
        ListElement {
            firstName: "Erwan"
            lastName: "Castex"
            favorite: true
        }
        // ...
    }

    TextField {
        id: textField
        anchors { top: parent.top; left: parent.left; right: parent.right }
        height: implicitHeight
    }

    SortFilterProxyModel {
        id: personProxyModel
        sourceModel: personModel
        filters: RegExpFilter {
            roleName: "lastName"
            pattern: textField.text
            caseSensitivity: Qt.CaseInsensitive
        }
        sorters: StringSorter { roleName: "firstName" }
    }

    ListView {
        anchors { top: textField.bottom; bottom: parent.bottom; left: parent.left; right: parent.right }
        model: personProxyModel
        delegate: Text { text: model.firstName + " " + model.lastName}
    }
}

Here the ListView will only show elements that contains the content of the TextField in their lastName role.

  • But you can also achieve more complex filtering or sorting with multiple filters and sorters:
    SortFilterProxyModel {
        id: personProxyModel
        sourceModel: personModel
        filters: [
            ValueFilter {
                enabled: onlyShowFavoritesCheckbox.checked
                roleName: "favorite"
                value: true
            },
            AnyOf {
                RegExpFilter {
                    roleName: "lastName"
                    pattern: textField.text
                    caseSensitivity: Qt.CaseInsensitive
                }
                RegExpFilter {
                    roleName: "firstName"
                    pattern: textField.text
                    caseSensitivity: Qt.CaseInsensitive
                }
            }
        ]
        sorters: [
            RoleSorter { roleName: "favorite"; sortOrder: Qt.DescendingOrder },
            StringSorter { roleName: "firstName" },
            StringSorter { roleName: "lastName" }
        ]
    }

This will show in the corresponding ListView only the elements where the firstName or the lastName match the text entered in the textField, and if the onlyShowFavoritesCheckbox is checked it will aditionnally filter the elements where favorite is true. The favorited elements will be shown first and all the elements are sorted by firstName and then lastName.

License

This library is licensed under the MIT License.

Documentation

This component is a subclass of QSortFilterProxyModel, to use it, you need to set the sourceModel property to a QAbstractItemModel* with correct role names. This means you can use it with custom c++ models or ListModel, but not with JavaScript models like arrays, integers or object instances.

The complete documentation reference is available here: https://okcerg.github.io/SortFilterProxyModel/

Contributing

Don't hesitate to open an issue about a suggestion, a bug, a lack of clarity in the documentation, etc.

Pull requests are also welcome, if it's a important change you should open an issue first though.