Strange behavior QTreeView
I have some difficult to understanding how the QTreeView work with QAbstractItemModel and what functions it calls.
To test, I created a simple class that inherits from QAbstractItemModel and contains a tree (using QStandardItemModel). My class simply calls the function of QStandardItemModel. For example, for data ():
- return sourceModel()->data(index, role);
I have overloaded functions that normally should be:
I also draw my tree with a simple recursive function that uses data(), hasChildren(), rowCoun () and index() functions:
Unfortunately, when I try to view my tree in a QTreeView, nothing is displayed (while the QStandardItemModel displayed properly).
An idea of what I forgot?
PS: I also tried using a QAbstractProxyModel unsuccessfully while a QSortFilterProxyModel works.
Thanks for the answer
All QModelIndex was created using sourceModel() in index() function et QModelIndex::model() return the internal QStandardItemModel. I have shown only data() function but I have wrote all listed functions (index, parent, data, rowCount, columnCount and flags) with similar implementation.
I think the problem is not there (or so I did not understand)
I have read the doc.
yes, it’s a proxy that I want to implement. My ultimate goal is to convert a TableModel in TreeModel using two columns of the table (one for id and one for the id of the parent). I have implemented this class, starting from QAbstractProxyModel but it didn’t work. Yet when I was testing my tree generated, it was correct. So I tried starting from QAbstractItemModel. But it did not work either. So I tried too a QAbstractItemModel which is supposed to be a simple warper of model. But this doesn’t work (this is the code that I gave).
So I guess I missed something but I can not find anything.
(I have write a class to create a new tree model from table model ans that work. But here, I want to map table and tree items)
Sorry, my English is not perfect and I was not necessarily clear.
My model is supposed to do anything, just call the functions of the model. And when I call the function drawTree(), the tree displays correctly. There is no position in my model proxy.
- return sourceModel()->index(row, column, parent);
- return sourceModel()->parent(child);
- return sourceModel()->data(index, role);
- int TableToTreeProxyModel::rowCount(const QModelIndex &parent) const
- return sourceModel()->rowCount(parent);
- int TableToTreeProxyModel::columnCount(const QModelIndex &parent) const
- return sourceModel()->columnCount(parent);
- return sourceModel()->flags(index);
To create tree & proxy:
- // tree
- QStandardItem* root = tree->invisibleRootItem();
- // proxy
- proxy = new TableToTreeProxyModel;
To draw :
I use… nothing in particular. I take a QStandardItem table for source and QStandardItem tree for destination. Conversion was performed with this :
- QList<int> rows;
- for (int row = 0; row < src->rowCount(); ++row)
- //qDebug() << " row" << id << src->data(src->index(row, 1));
- if (src->data(parentIdRow(src, row)) == id)
- return rows;
- QList<int> rows = rowByParentID(src, parent);
- for (int row = 0; row < rows.size(); ++row)
- toTree(src, child, idRow(src, rows[row]));
- table = new QStandardItemModel(5, 3, this);
- tree2 = new QStandardItemModel(this);
- toTree(table, tree2->invisibleRootItem());
(I don’t use free functions in fact, but it’s not important here)
Problems with this approach is : data was duplicated and I must to create symetric functions to convert tree to table (if tree was modified, for update data in table)
What I want to do has been added to Qt 4.8 (QIdentityProxyModel : http://doc.trolltech.com/4.8-snapshot/qidentityproxymodel.html)
However, this class uses the createIndex() function, which is protected (http://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/itemviews/qidentityproxymodel.cpp#line259).
- return d->model->createIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer());
I implementing this class using the index() function, it’s work for tables but not for trees.
- return sourceModel()->index(proxyIndex.row(), proxyIndex.column());
I think this will work for table to tree proxy but not for symetric proxy (tree to table proxy).
If someone was ideas.
I don’t know QIdentityProxyModel.
But as you only have a standard proxy model in 4.7, you have to have an index, that is created on this model and do your own mapping.
You index function may never return an index from the source model directly.
The function you mention can be called inside methods of the proxy model, e.g. for mapToSource, where it also works in 4.7.