// flaggableproxy.h #ifndef FLAGGABLEPROXY_H #define FLAGGABLEPROXY_H #include class FlaggableProxy : public QIdentityProxyModel { Q_OBJECT public: explicit FlaggableProxy(QObject *parent=nullptr); virtual ~FlaggableProxy() = default; virtual Qt::ItemFlags flags(const QModelIndex & index) const override; virtual void setFlagForColumn(qint32 col, Qt::ItemFlags val); virtual void removeFlagForColumn(qint32 col); virtual Qt::ItemFlags flagsForColumn(qint32 col) const; virtual void setFlagForRow(qint32 row, Qt::ItemFlags val); virtual void removeFlagForRow(qint32 row); virtual Qt::ItemFlags flagsForRow(qint32 row) const; virtual void setFlag(qint32 row, qint32 col, Qt::ItemFlags val); virtual void removeFlag(qint32 row, qint32 col); virtual Qt::ItemFlags getFlag(qint32 row, qint32 col)const; virtual void clearFlags(); private: QHash m_flagStorage; QHash m_rowFlagStorage; QHash m_colFlagStorage; }; #endif // FLAGGABLEPROXY_H // flaggableproxy.cpp #include "flaggableproxy.h" FlaggableProxy::FlaggableProxy(QObject *parent) : QIdentityProxyModel(parent) { } Qt::ItemFlags FlaggableProxy::flags(const QModelIndex & idx) const { const QModelIndex index = mapToSource(idx); const qint64 hashIndex = (static_cast(index.row()) << 32) | static_cast(index.column()); if (m_flagStorage.contains(hashIndex)) return m_flagStorage.value(hashIndex); if (m_rowFlagStorage.contains(index.row())) return m_rowFlagStorage.value(index.row()); if (m_colFlagStorage.contains(index.column())) return m_colFlagStorage.value(index.column()); return QIdentityProxyModel::flags(idx); } void FlaggableProxy::setFlagForColumn(qint32 col, Qt::ItemFlags val) { m_colFlagStorage[col] = val; } void FlaggableProxy::removeFlagForColumn(qint32 col) { m_colFlagStorage.remove(col); } Qt::ItemFlags FlaggableProxy::flagsForColumn(qint32 col) const { return m_colFlagStorage.value(col, QIdentityProxyModel::flags(index(0, col))); } void FlaggableProxy::setFlagForRow(qint32 row, Qt::ItemFlags val) { m_rowFlagStorage[row] = val; } void FlaggableProxy::removeFlagForRow(qint32 row) { m_colFlagStorage.remove(row); } Qt::ItemFlags FlaggableProxy::flagsForRow(qint32 row) const { return m_rowFlagStorage.value(row, QIdentityProxyModel::flags(index(row, 0))); } void FlaggableProxy::setFlag(qint32 row, qint32 col, Qt::ItemFlags val) { const qint64 hashIndex = (static_cast(row) << 32) | static_cast(col); m_flagStorage[hashIndex] = val; } void FlaggableProxy::removeFlag(qint32 row, qint32 col) { const qint64 hashIndex = (static_cast(row) << 32) | static_cast(col); m_flagStorage.remove(hashIndex); } Qt::ItemFlags FlaggableProxy::getFlag(qint32 row, qint32 col) const { const qint64 hashIndex = (static_cast(row) << 32) | static_cast(col); return m_flagStorage.value(hashIndex, QIdentityProxyModel::flags(index(row, col))); } void FlaggableProxy::clearFlags() { m_flagStorage.clear(); m_rowFlagStorage.clear(); m_colFlagStorage.clear(); }