PDA

View Full Version : recursively calling QAbstractItemModel::beginInsertRows()



mule
23rd November 2011, 15:14
Is it permitted to recursively call QAbstractItemModel::beginInsertRows() ?
That is: call beginInsertRows() and then, before calling endInsertRows(), call beginInsertRows() again?

Eventually, there will be a call to endInsertRows() for every call to beginInsertRows().

The application is to pre-populate a tree with a view of a file system, including all of its sub-directories. There is also other special, proprietary information that is merged with the directory/file names.

I am attempting to do something like this:


void vInsertChildren( const QModelIndex & a_rtItemIndex,
const QList< QFileInfo > & a_ratChildEntries )
{
if ( ! a_ratChildEntries.isEmpty() ) {
// begin inserting children
beginInsertRows( a_rtItemIndex, 0, 0 + a_ratChildEntries.count() - 1 );

// for each child ...
for ( int i = 0; i <= a_ratChildEntries.count() - 1; ++ i ) {
// insert child
[insert row]

// check for grandchildren

QDir tGrandchildDir ( a_ratChildEntries[ i ].filePath() );
QList< QFileInfo > atGrandchildEntries;

atGrandchildEntries = tGrandchildDir.entryInfoList(
QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files,
QDir::Name | QDir::DirsFirst | QDir::IgnoreCase
);

if ( ! atGrandchildEntries.isEmpty() ) {
// insert grandchildren
vInsertChildren(
index( 0, 0, a_rtItemIndex ),
atGrandchildEntries
);
}


}

// end inserting children
endInsertRows();
}
}

The same question applies to beginRemoveRows() / endRemoveRows().