Strange behavior QTreeView
Hello everyone
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:
- {
- qDebug() << t + tree->data(parent).toString();
- if (tree->hasChildren(parent))
- {
- for (int row = 0; row < tree->rowCount(parent); ++row )
- {
- drawTree(tree, tree->index(row, 0, parent), level+1);
- }
- }
- }
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?
thank you
PS: I also tried using a QAbstractProxyModel unsuccessfully while a QSortFilterProxyModel works.
10 replies
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 think, you didn’t want to implement a proxy model, right?
have a look at the Qt docs [doc.qt.nokia.com] or, if you understand German, at the German wiki [developer.qt.nokia.com]
Thanks
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);
- }
- {
- return sourceModel()->rowCount(parent);
- }
- {
- return sourceModel()->columnCount(parent);
- }
- {
- return sourceModel()->flags(index);
- }
To create tree & proxy:
- // tree
- root->appendRow(toto);
- toto->appendRow(titi);
- titi->appendRow(tata);
- root->appendRow(tutu);
- pokemon->appendRow(tyty);
- // proxy
- proxy = new TableToTreeProxyModel;
- proxy->setSourceModel(tree);
To draw :
I use… nothing in particular. I take a QStandardItem table for source and QStandardItem tree for destination. Conversion was performed with this :
- {
- for (int row = 0; row < src->rowCount(); ++row)
- {
- //qDebug() << " row" << id << src->data(src->index(row, 1));
- if (src->data(parentIdRow(src, row)) == id)
- rows.append(row);
- }
- return rows;
- }
- {
- for (int row = 0; row < rows.size(); ++row)
- {
- dst->appendRow(child);
- toTree(src, child, idRow(src, rows[row]));
- }
- }
- 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.
Yhanks
Hi,
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.
You must log in to post a reply. Not a member yet? Register here!



