Can you say "recursion"?
{
stream << level << index.row() << index.column() << index.data() << endl( stream );
if ( model.hasChildren( index ) )
{
int nRows = model.rowCount( index );
int nCols = model.columnCount( index );
for ( int nRow = 0; nRow < nRows; ++nRow )
{
for ( int nCol = 0; nCol < nCols; ++nCol )
{
WriteIndex( model, model.index( nRow, nCol, index ), stream, level + 1 );
}
}
}
}
void WriteIndex( const QAbstractItemModel & model, const QModelIndex & index, QTextStream & stream, int level )
{
stream << level << index.row() << index.column() << index.data() << endl( stream );
if ( model.hasChildren( index ) )
{
int nRows = model.rowCount( index );
int nCols = model.columnCount( index );
for ( int nRow = 0; nRow < nRows; ++nRow )
{
for ( int nCol = 0; nCol < nCols; ++nCol )
{
WriteIndex( model, model.index( nRow, nCol, index ), stream, level + 1 );
}
}
}
}
To copy to clipboard, switch view to plain text mode
and you start the whole thing off with the model index that is the root of your tree and level = 0.
Since the QModelIndex::data() method returns a QVariant, you'll probably need to call one of the QVariant::to...() methods when writing.
Bookmarks