QAbstractProxyModel use with QTreeView
-
Hi. Im trying to do the following exercise:
Proxy QStandardItemModel into tree representation. First column is a roots for other columns. But Proxy subclassing unexpectedly undocumented and difficult to understand (without doc or examples).All i got for now is i should properly define parent method (since i dont use internal pointers)
class StandardTreeProxy : public QAbstractProxyModel { // QAbstractItemModel interface public: QModelIndex index(int row, int column, const QModelIndex &parent) const { return createIndex(row, column); } QModelIndex parent(const QModelIndex &child) const { auto sourceIndex = mapToSource(child); auto proxyIndex = mapFromSource(sourceModel()->index(sourceIndex.row(), 0)); if(child.row() == proxyIndex.row()) { return QModelIndex(); } return proxyIndex; } int rowCount(const QModelIndex &parent) const { return sourceModel()->rowCount() * sourceModel()->columnCount(); } int columnCount(const QModelIndex &parent) const { return 1; } // QAbstractProxyModel interface public: QModelIndex mapToSource(const QModelIndex &proxyIndex) const { int row = std::round(proxyIndex.row() / sourceModel()->columnCount()); int col = proxyIndex.row() % sourceModel()->columnCount(); return sourceModel()->index(row, col); } QModelIndex mapFromSource(const QModelIndex &sourceIndex) const { int row = sourceModel()->columnCount() * sourceIndex.row() + sourceIndex.column(); int col = 0; if(row == 0) { return index(row, col, QModelIndex()); } return index(row, col, createIndex(0, 0)); } };
original model
and proxy
Im trying this, but all i got is columnts placed in a rows.
Is it possible?
Should i owerride another method from proxy? -
@SergeyK12
Are you aware that in aQStandardItemModel
you can create a parent-child hierarchy? E.g. start from https://doc.qt.io/qt-6/qstandarditemmodel.html#details and QStandardItemModel::insertRow(int row, const QModelIndex &parent = QModelIndex()) etc.So why don't you store your model like that if it's hierarchical?
Unless you mean you start with a
QStandardItemModel
which is flat and multi-column like the first one, for whatever reason, and want to display that in the second way. For that yes a proxy like yours would be good. -
I found out, that rowCount should return count for child row.
So it should be like thisint rowCount(const QModelIndex &parent) const { if(!parent.isValid()) { return sourceModel()->rowCount(); } return sourceModel()->columnCount() - 1; }
which reveal problems with parents ( now it only 1 row in a view, all paret idx is invalid)
-
@SergeyK12 said in QAbstractProxyModel use with QTreeView:
Proxy QStandardItemModel into tree representation.
Why? There is no need for a proxy here at all.
-
I suppose that mapping function is incorrect (since it direct mapping, without tree structure). QModelIndex::parent crash the app.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const { int row = std::round(proxyIndex.row() / sourceModel()->columnCount()); int col = proxyIndex.row() % sourceModel()->columnCount(); return sourceModel()->index(row, col); } QModelIndex mapFromSource(const QModelIndex &sourceIndex) const { int row = sourceModel()->columnCount() * sourceIndex.row() + sourceIndex.column(); int col = 0; if(row == 0) { return index(row, col, QModelIndex()); } return index(row, col, createIndex(0, 0)); }
-
I still don't understand what you're trying to achieve - QStandardItemModel can be used in a QTreeView without any problems. So why trying to create a proxy model for this then? What's the gain?
-
@Christian-Ehrlicher
Sorry, i cant find a way to show this:
as a tree
without proxy
Possibly a should store data other way or use roles? -
@SergeyK12
Are you aware that in aQStandardItemModel
you can create a parent-child hierarchy? E.g. start from https://doc.qt.io/qt-6/qstandarditemmodel.html#details and QStandardItemModel::insertRow(int row, const QModelIndex &parent = QModelIndex()) etc.So why don't you store your model like that if it's hierarchical?
Unless you mean you start with a
QStandardItemModel
which is flat and multi-column like the first one, for whatever reason, and want to display that in the second way. For that yes a proxy like yours would be good. -