KCategorizedSortFilterProxyModel Class

This class lets you categorize a view. It is meant to be used along with KCategorizedView class. More...

Header: #include <KCategorizedSortFilterProxyModel>
CMake: find_package(KF6 REQUIRED COMPONENTS ItemViews)
target_link_libraries(mytarget PRIVATE KF6::ItemViews)
Inherits: QSortFilterProxyModel

Public Types

enum AdditionalRoles { CategoryDisplayRole, CategorySortRole }

Public Functions

KCategorizedSortFilterProxyModel(QObject *parent = nullptr)
bool isCategorizedModel() const
void setCategorizedModel(bool categorizedModel)
void setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison)
bool sortCategoriesByNaturalComparison() const
int sortColumn() const
Qt::SortOrder sortOrder() const

Reimplemented Public Functions

virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override

Protected Functions

virtual int compareCategories(const QModelIndex &left, const QModelIndex &right) const
virtual bool subSortLessThan(const QModelIndex &left, const QModelIndex &right) const

Reimplemented Protected Functions

virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const override

Detailed Description

In general terms all you need to do is to reimplement subSortLessThan() and compareCategories() methods. In order to make categorization work, you need to also call setCategorizedModel() class to enable it, since the categorization is disabled by default.

See also KCategorizedView.

Member Type Documentation

enum KCategorizedSortFilterProxyModel::AdditionalRoles

ConstantValueDescription
KCategorizedSortFilterProxyModel::CategoryDisplayRole0x17CE990AThis role is used for asking the category to a given index
KCategorizedSortFilterProxyModel::CategorySortRole0x27857E60This role is used for sorting categories. You can return a string or a long long value. Strings will be sorted alphabetically while long long will be sorted by their value. Please note that this value won't be shown on the view, is only for sorting purposes. What will be shown as "Category" on the view will be asked with the role CategoryDisplayRole.

Member Function Documentation

KCategorizedSortFilterProxyModel::KCategorizedSortFilterProxyModel(QObject *parent = nullptr)

[virtual protected] int KCategorizedSortFilterProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const

This method compares the category of the left index with the category of the right index.

Internally and if not reimplemented, this method will ask for left and right models for role CategorySortRole. In order to correctly sort categories, the data() method of the model should return a qlonglong (or numeric) value, or a QString object. QString objects will be sorted with QString::localeAwareCompare if sortCategoriesByNaturalComparison() is true.

Note: Please have present that: QString(QChar(QChar::ObjectReplacementCharacter)) > QString(QChar(QChar::ReplacementCharacter)) > [ all possible strings ] > QString();

This means that QString() will be sorted the first one, while QString(QChar(QChar::ObjectReplacementCharacter)) and QString(QChar(QChar::ReplacementCharacter)) will be sorted in last position.

Warning: Please note that data() method of the model should return always information of the same type. If you return a QString for an index, you should return always QStrings for all indexes for role CategorySortRole in order to correctly sort categories. You can't mix by returning a QString for one index, and a qlonglong for other.

Note: If you need a more complex layout, you will have to reimplement this method.

Returns A negative value if the category of left should be placed before the category of right. 0 if left and right are on the same category, and a positive value if the category of left should be placed after the category of right.

bool KCategorizedSortFilterProxyModel::isCategorizedModel() const

Returns whether the model is categorized or not. Disabled by default.

[override virtual protected] bool KCategorizedSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const

Reimplements: QSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const.

Overridden from QSortFilterProxyModel. If you are subclassing KCategorizedSortFilterProxyModel, you will probably not need to reimplement this method.

It calls compareCategories() to sort by category. If the both items are in the same category (i.e. compareCategories returns 0), then subSortLessThan is called.

Returns true if the item left is less than the item right when sorting.

Warning: You usually won't need to reimplement this method when subclassing from KCategorizedSortFilterProxyModel.

void KCategorizedSortFilterProxyModel::setCategorizedModel(bool categorizedModel)

Enables or disables the categorization feature.

categorizedModel whether to enable or disable the categorization feature.

See also isCategorizedModel().

void KCategorizedSortFilterProxyModel::setSortCategoriesByNaturalComparison(bool sortCategoriesByNaturalComparison)

Set if the sorting using CategorySortRole will use a natural comparison in the case that strings were returned. If enabled, QCollator will be used for sorting.

sortCategoriesByNaturalComparison whether to sort using a natural comparison or not.

See also sortCategoriesByNaturalComparison().

[override virtual] void KCategorizedSortFilterProxyModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)

Reimplements: QSortFilterProxyModel::sort(int column, Qt::SortOrder order).

Overridden from QSortFilterProxyModel. Sorts the source model using column for the given order.

bool KCategorizedSortFilterProxyModel::sortCategoriesByNaturalComparison() const

Returns whether it is being used a natural comparison for sorting. Enabled by default.

See also setSortCategoriesByNaturalComparison().

int KCategorizedSortFilterProxyModel::sortColumn() const

Returns the column being used for sorting.

Qt::SortOrder KCategorizedSortFilterProxyModel::sortOrder() const

Returns the sort order being used for sorting.

[virtual protected] bool KCategorizedSortFilterProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const

This method has a similar purpose as lessThan() has on QSortFilterProxyModel. It is used for sorting items that are in the same category.

Returns true if the item left is less than the item right when sorting.