Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // multisort.h
- #include <QSortFilterProxyModel>
- #include <QList>
- #include <QPair>
- class MultySortProxy : public QSortFilterProxyModel
- {
- Q_OBJECT
- Q_DISABLE_COPY(MultySortProxy)
- public:
- explicit MultySortProxy(QObject *parent = nullptr);
- virtual ~MultySortProxy();
- void addSortPriority(int col, int role = Qt::DisplayRole);
- void revoveSortPriority(int col, int role = Qt::DisplayRole);
- void clearSortPriority();
- void insertSortPriority(int level, int col, int role = Qt::DisplayRole);
- protected:
- virtual bool lessThan(const QModelIndex & source_left, const QModelIndex & source_right) const override;
- protected:
- QList<QPair<int, int>> m_sortPriority;
- };
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // multisort.cpp
- #include "multisort.h"
- #include <QVariant>
- MultySortProxy::MultySortProxy(QObject *parent)
- : QSortFilterProxyModel(parent)
- {}
- MultySortProxy::~MultySortProxy(){}
- void MultySortProxy::addSortPriority(int col, int role)
- {
- m_sortPriority.append(QPair<int, int>(col, role));
- }
- void MultySortProxy::revoveSortPriority(int col, int role)
- {
- m_sortPriority.removeAt(m_sortPriority.indexOf(QPair<int, int>(col, role)));
- }
- void MultySortProxy::clearSortPriority()
- {
- m_sortPriority.clear();
- }
- void MultySortProxy::insertSortPriority(int level, int col, int role)
- {
- m_sortPriority.insert(level, QPair<int, int>(col, role));
- }
- bool MultySortProxy::lessThan(const QModelIndex & source_left, const QModelIndex & source_right) const
- {
- for (auto i = m_sortPriority.constBegin(); i != m_sortPriority.constEnd();++i){
- const QVariant leftData = source_left.model()->index(source_left.row(), i->first, source_left.parent()).data(i->second);
- const QVariant rightData = source_right.model()->index(source_right.row(), i->first, source_right.parent()).data(i->second);
- if (leftData < rightData)
- return true;
- if (rightData < leftData)
- return false;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement