Merge PR #506: Add sorting capability to generic sortfilter proxy model
This commit is contained in:
commit
ad5d1f222b
@ -42,6 +42,26 @@ void SortFilterProxyModel::setFilterList(const QStringList &filterList)
|
||||
}
|
||||
}
|
||||
|
||||
QString SortFilterProxyModel::sortRoleName() const
|
||||
{
|
||||
return m_sortRoleName;
|
||||
}
|
||||
|
||||
void SortFilterProxyModel::setSortRoleName(const QString &sortRoleName)
|
||||
{
|
||||
if (m_sortRoleName != sortRoleName) {
|
||||
m_sortRoleName = sortRoleName;
|
||||
emit sortRoleNameChanged();
|
||||
sort(0, sortOrder());
|
||||
}
|
||||
}
|
||||
|
||||
void SortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
|
||||
{
|
||||
sort(0, sortOrder);
|
||||
emit sortOrderChanged();
|
||||
}
|
||||
|
||||
QVariant SortFilterProxyModel::data(int row, const QString &role) const
|
||||
{
|
||||
int roleId = roleNames().key(role.toUtf8());
|
||||
@ -60,3 +80,13 @@ bool SortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &s
|
||||
}
|
||||
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
|
||||
}
|
||||
|
||||
bool SortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
|
||||
{
|
||||
int sortRole = sourceModel()->roleNames().key(m_sortRoleName.toUtf8());
|
||||
|
||||
QVariant left = sourceModel()->data(source_left, sortRole);
|
||||
QVariant right = sourceModel()->data(source_right, sortRole);
|
||||
|
||||
return left <= right;
|
||||
}
|
||||
|
||||
@ -8,6 +8,8 @@ class SortFilterProxyModel : public QSortFilterProxyModel
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString filterRoleName READ filterRoleName WRITE setFilterRoleName NOTIFY filterRoleNameChanged)
|
||||
Q_PROPERTY(QStringList filterList READ filterList WRITE setFilterList NOTIFY filterListChanged)
|
||||
Q_PROPERTY(QString sortRoleName READ sortRoleName WRITE setSortRoleName NOTIFY sortRoleNameChanged)
|
||||
Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
|
||||
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
|
||||
|
||||
public:
|
||||
@ -19,19 +21,28 @@ public:
|
||||
QStringList filterList() const;
|
||||
void setFilterList(const QStringList &filterList);
|
||||
|
||||
QString sortRoleName() const;
|
||||
void setSortRoleName(const QString &sortRoleName);
|
||||
|
||||
void setSortOrder(Qt::SortOrder sortOrder);
|
||||
|
||||
Q_INVOKABLE QVariant data(int row, const QString &role) const;
|
||||
|
||||
signals:
|
||||
void filterRoleNameChanged();
|
||||
void filterListChanged();
|
||||
void sortRoleNameChanged();
|
||||
void sortOrderChanged();
|
||||
void countChanged();
|
||||
|
||||
protected:
|
||||
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
|
||||
bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
|
||||
|
||||
private:
|
||||
QString m_filterRoleName;
|
||||
QStringList m_filterList;
|
||||
QString m_sortRoleName;
|
||||
};
|
||||
|
||||
#endif // SORTFILTERPROXYMODEL_H
|
||||
|
||||
Reference in New Issue
Block a user