From 9efc1fb4ac7982f105a13781fccff74a61907601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Arve=20S=C3=A6ther?= Date: Thu, 29 May 2025 12:13:57 +0200 Subject: Provide a way to sieve data in QML through the SortFilterProxyModel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enhance QSortFilterProxyModel to be available in QML with changes as mentioned below and export the type as SortFilterProxyModel (Note: adopted most of these features from existing project - https://github.com/oKcerG/SortFilterProxyModel) * Inherit QQmlSortFilterProxyModelPrivate from QSortFilterProxyModelHelper and use the mapping logic of source to proxy indexes from it. * Allow the model to be configurable with multiple filters and sorters. The filter and sorter components shall be inherited from QQmlFilterBase and QQmlSorterBase respectively. The components are maintained within the respective compositor classes. The filter and sorting operation from QQmlSortFilterProxyModel will be forwarded to the compositor which then iterate through the configured components to sieve the data. This patch allows the following filters and sorters configurable in SFPM, Filters: ValueFilter - Filters the data that matching with the provided value or role name or combined together if both are specified. FunctionFilter - Filters the data according to the result of the evaluated js method. Sorters: RoleSorter - Sorts the data according to the provided role name. StringSorter - Sorts the data by considering the locale. FunctionSorter - Sorts the data according to the evaluated js method. * Add support for 'enabled', 'column' property for both filters and sorters, and 'priority' property for the sorters. Task-number: QTBUG-71348 Change-Id: I65b84936642e5f0f382d83413648d2c6794c18aa Reviewed-by: Jan Arve Sæther --- .../qml-sortfilterproxymodel.qml | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/qmlmodels/doc/snippets/qml/sortfilterproxymodel/qml-sortfilterproxymodel.qml (limited to 'src/qmlmodels/doc/snippets') diff --git a/src/qmlmodels/doc/snippets/qml/sortfilterproxymodel/qml-sortfilterproxymodel.qml b/src/qmlmodels/doc/snippets/qml/sortfilterproxymodel/qml-sortfilterproxymodel.qml new file mode 100644 index 0000000000..9f7c2e508f --- /dev/null +++ b/src/qmlmodels/doc/snippets/qml/sortfilterproxymodel/qml-sortfilterproxymodel.qml @@ -0,0 +1,57 @@ +// Copyright (C) 2025 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//![0] +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + width: 640 + height: 480 + visible: true + title: qsTr("Sort Filter Proxy Model") + + //! [sfpm-usage] + ListModel { + id: listModel + ListElement { name: "Adan"; age: 25; department: "Process"; pid: 209711; country: "Norway" } + ListElement { name: "Hannah"; age: 48; department: "HR"; pid: 154916; country: "Germany" } + ListElement { name: "Divina"; age: 63; department: "Marketing"; pid: 158038; country: "Spain" } + ListElement { name: "Rohith"; age: 35; department: "Process"; pid: 202582; country: "India" } + ListElement { name: "Latesha"; age: 23; department: "Quality"; pid: 232582; country: "UK" } + } + + SortFilterProxyModel { + id: ageFilterModel + model: listModel + filters: [ + FunctionFilter { + roleData: QtObject { property int age } + function filter(data: QtObject) : bool { + return data.age > 30 + } + } + ] + sorters: [ + RoleSorter { roleName: "department" } + ] + } + + ListView { + anchors.fill: parent + clip: true + model: sfpm + delegate: Rectangle { + implicitWidth: 100 + implicitHeight: 50 + border.width: 1 + Text { + text: name + anchors.centerIn: parent + } + } + } + //! [sfpm-usage] +} + +//![0] -- cgit v1.2.3