-Ch1--------------Pomp1..........1000
| |
| -----------------Pomp2..........3000
|
---Ch2------------Flowmeter.....2000
|
...
Doing that will be harder than this:
-Ch1
| |-----------------Pomp1..........1000
| |-----------------Pomp2..........3000
|
-Ch2
| |-----------------Flowmeter.....2000
|
...
If you do it your way, the tree structure is a bit hard to keep track of. Each top level item would either have no children or one or more children, but if it has no children, it has more than one column. So the mapping would be hard - if your index is the first child of the channel node, then it is at the same level in the tree, otherwise it is at a level below the channel node.
If you do it the second way, the code would look something like this:
// >> Warning, untested!
{
if ( index.isValid() )
{
int count = 0;
while ( parentIndex.isValid() )
{
count++;
lastValidParent = parentIndex;
parentIndex = parentIndex.parent();
}
// The column in the source table corresponds to the index's column plus the number of times we moved up the tree
int column = index.column() + count;
// The row corresponds to the row of the last valid parent (the channel row in the tree) plus the index's row
// (which corresponds to where the index is below the channel node)
int row = lastValidParent.row() + index.row();
sourceIndex
= sourceModel
()->index
( row, column,
QModelIndex() );
}
return sourceIndex;
}
// >> Warning, untested!
QModelIndex MyProxy::mapToSource( const QModelIndex & index )
{
QModelIndex sourceIndex;
if ( index.isValid() )
{
int count = 0;
QModelIndex lastValidParent = index;
QModelIndex parentIndex = index.parent();
while ( parentIndex.isValid() )
{
count++;
lastValidParent = parentIndex;
parentIndex = parentIndex.parent();
}
// The column in the source table corresponds to the index's column plus the number of times we moved up the tree
int column = index.column() + count;
// The row corresponds to the row of the last valid parent (the channel row in the tree) plus the index's row
// (which corresponds to where the index is below the channel node)
int row = lastValidParent.row() + index.row();
sourceIndex = sourceModel()->index( row, column, QModelIndex() );
}
return sourceIndex;
}
To copy to clipboard, switch view to plain text mode
I think you can probably figure out how to implement mapFromSource(), since you will need to do this to build the tree in the first place.
Bookmarks