Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////
- // ColumnsToRolesProxy.h
- //////////////////////////////////////////////////////////////////////////
- #ifndef COLUMNSTOROLESPROXY_H
- #define COLUMNSTOROLESPROXY_H
- #include <QIdentityProxyModel>
- class ColumnsToRolesProxy : public QIdentityProxyModel
- {
- Q_OBJECT
- public:
- ColumnsToRolesProxy(QObject *parent=nullptr);
- virtual ~ColumnsToRolesProxy() = default;
- virtual QHash<int, QByteArray> roleNames() const override;
- virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
- virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
- virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
- enum{
- FirstColumnShift = 10
- };
- };
- #endif // COLUMNSTOROLESPROXY_H
- //////////////////////////////////////////////////////////////////////////
- // ColumnsToRolesProxy.cpp
- //////////////////////////////////////////////////////////////////////////
- #include "ColumnsToRolesProxy.h"
- ColumnsToRolesProxy::ColumnsToRolesProxy(QObject *parent)
- : QIdentityProxyModel(parent)
- {}
- QHash<int, QByteArray> ColumnsToRolesProxy::roleNames() const
- {
- QHash<int, QByteArray> result = QIdentityProxyModel::roleNames();
- QList<int> baseRoles = result.keys();
- baseRoles.removeAll(Qt::DisplayRole);
- for (int i = 0; i < sourceModel()->columnCount();++i){
- result[(i << FirstColumnShift) | Qt::DisplayRole] = sourceModel()->headerData(i, Qt::Horizontal, Qt::DisplayRole).toByteArray();
- for (auto j = baseRoles.constBegin(); j != baseRoles.constEnd(); ++j) {
- result.insert((i << FirstColumnShift) | *j, "col" + QByteArray::number(i) + result.value(*j));
- }
- }
- return result;
- }
- QVariant ColumnsToRolesProxy::data(const QModelIndex &index, int role) const
- {
- return sourceModel()->data(sourceModel()->index(index.row(), role >> FirstColumnShift, index.parent()), role & ((1 << FirstColumnShift) - 1));
- }
- bool ColumnsToRolesProxy::setData(const QModelIndex &index, const QVariant &value, int role)
- {
- return sourceModel()->setData(sourceModel()->index(index.row(), role >> FirstColumnShift, index.parent()), value, role & ((1 << FirstColumnShift) - 1));
- }
- int ColumnsToRolesProxy::columnCount(const QModelIndex &parent) const
- {
- return qMin(1, QIdentityProxyModel::columnCount(parent));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement