FunctionSorter QML Type
Sorts data in a SortFilterProxyModel based on the evaluation of the designated 'compare' method. More...
| Import Statement: | import QtQml.Models |
| Since: | Qt 6.10 |
| Inherits: | |
| Status: | Deprecated since 6.12 |
This type is deprecated since QtQml.Models 6.12. We strongly advise against using it in new code.
Detailed Description
FunctionSorter allows user to define the designated 'compare' method and it will be evaluated to sort the data. The method takes two arguments (lhs and rhs) of the specified parameter type and the data can be accessed as below for evaluation,
SortFilterProxyModel {
sourceModel: model
sorters: [
FunctionSorter {
id: functionSorter
component RoleData: QtObject {
property real age
}
function compare(lhsData: RoleData, rhsData: RoleData) : int {
return (lhsData.age < rhsData.age) ? -1 : ((lhsData === rhsData.age) ? 0 : 1)
}
}
]
}Warning: Both parameters of the compare method must carry an explicit type annotation, as lhsData and rhsData do in the example above, and both annotations must resolve to the same type. The compare method written the ordinary JavaScript way, without parameter type annotations, e.g. function compare(lhsData, rhsData) : int { ... }, compiles without any warning but fails with comparision. FunctionSorter relies on the annotated type to know what kind of role-data object to instantiate and populate for lhsData and rhsData; without it, there is nothing to instantiate.
Note: The sort method is deprecated. Rename it to compare.
Note: The compare function must have exactly two explicitly type-annotated parameters of matching types and an explicit : int return type. If these requirements are not met, the sorter emits a warning and returns early.
Note: The user needs to explicitly invoke SortFilterProxyModel::invalidateSorter whenever any external qml property used within the designated 'compare' method changes. This behaviour is subject to change in the future, like implicit invalidation and thus the user doesn't need to explicitly invoke SortFilterProxyModel::invalidateSorter.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.