The index() function shouldn't return a "blank" index like this... Index created by it are used by the view to iterate over your data and display it properly. The great big thing here is to clearly define how you associate model indexes with your underlying data. QModelIndex provides a way of associating either a void* or an integer which can be retrieved later on (mainly in functions like
data(), flags(), setData(), ... but not only). What's wrong with your code is that you discard the parent passed to createIndex()...
If this parent is not valid it means that the (row, col) are relative to the root of the tree. Otherwise they are children of the parent index passed. and QTreeView keeps adding children. To fix this just use this code (I'mm assuming that you really don't use any tree structure here...)
QModelIndex PlayersModel
::index(int row,
int column,
const QModelIndex
& parent
) {
if ( parent.isValid() )
return QModelIdex();
return createIndex(row, column);
}
QModelIndex PlayersModel::index(int row, int column, const QModelIndex& parent)
{
if ( parent.isValid() )
return QModelIdex();
return createIndex(row, column);
}
To copy to clipboard, switch view to plain text mode
Bookmarks