Quote Originally Posted by wysota View Post
The mapping is different on "0" level and different on all other levels. It is also different when mapping from the proxy to the source (and that's the problem). I managed to implement everything but the parent() method. In my opinion there is no way of backtracking indexes into the source model other than providing a structure (or two, to be exact) for mapping between indexes of the two models. That's ok as QSortFilterProxyModel does something similar but it is quite complex to implement as there are synchronization issues to consider.
I have implemented the topicissue without problems with the TreeItem and TreeModel (from Qt's source snippets) Here is a snippet from the adjusted TreeModel::setupModelData method which works:

Qt Code:
  1. QList<TreeItem*> parents;
  2. parents << parent;
  3.  
  4. QSqlTableModel table(this, db->returnConnection());
  5. table.setTable("edg_5_lvl_ext");
  6. table.select();
  7.  
  8. for (int row = 0; row < table.rowCount(); row++)
  9. {
  10. for (int column = 0; column < table.columnCount(); column++)
  11. {
  12. QList<QVariant> data;
  13. data << table.data(table.index(row,column, QModelIndex()),Qt::DisplayRole);
  14. parents.last()->appendChild(new TreeItem(data, parents.last()));
  15. parents << parents.last()->child(parents.last()->childCount()-1);
  16. }
  17. parents << parent;
  18. }
To copy to clipboard, switch view to plain text mode 

In the process I got some more insight into how the TreeModel class is made: primarily with QModelIndex::internalPointer ()

Maybe one could use the same method with a proxy for pointing to indexes from the sourceModel(). Here some brainstorming for the mapToSource method:

Qt Code:
  1. QModelIndex mapToSource(const QModelIndex &proxyIndex)
  2. {
  3. if (*(QModelIndex*) proxyIndex.internalPointer() == QModelIndex()) return QModelIndex();
  4. if (((QModelIndex*) proxyIndex.internalPointer())->column() == 0) return QModelIndex();
  5. return sourceModel()->index(proxyIndex.row(), proxyIndex.column());
  6. }
To copy to clipboard, switch view to plain text mode 

Something else wysota. I don't know what you had in mind when you said that you always miss one value: colum/row or parent. But maybe if you could use the qint32 from an index, you maybe could extract the unknown row from ((row+column)^2+row-column)/2 formula, provided you know the column.