Should I insert new songs inside the dropEvent of the viewport or in some overridden method of the model?
Where you insert depends on where you are storing your song list. If the only place it is stored is inside the model, then as you decode the mp3 metadata on the drop, you create a Song instance and use a custom method to add it to the array inside the model. In this case, your custom method must do this:
void MyModel::addSongs( const QList< Song * > & addSongs )
{
- call beginInsertRows()
- append the new Song * instance(s) to the end of the QList< Song * >
- call endInsertRows()
}
void MyModel::addSongs( const QList< Song * > & addSongs )
{
- call beginInsertRows()
- append the new Song * instance(s) to the end of the QList< Song * >
- call endInsertRows()
}
To copy to clipboard, switch view to plain text mode
Likewise, if you remove a song from the list:
void MyModel::removeSongs( const QList< Song * > & delSongs )
{
- for each Song in the list to remove
- find the row that contains it
- call beginRemoveRows()
- remove the Song * instance from the QList< Song * >
- call endRemoveRows()
}
void MyModel::removeSongs( const QList< Song * > & delSongs )
{
- for each Song in the list to remove
- find the row that contains it
- call beginRemoveRows()
- remove the Song * instance from the QList< Song * >
- call endRemoveRows()
}
To copy to clipboard, switch view to plain text mode
(I think each call to endRemoveRows() will result in an update to the views, so there might be a cleaner way to do this that minimizes the number of updates).
Alternatively, if you store the song list externally to the model (and simply pass in a pointer for the model to use), you can have a custom method that completely replaces the data after the songlist is modified:
void MyModel::updateSongList( QList< Song * > * songlist )
{
beginResetModel();
mpSonglist = songlist;
endResetModel();
}
void MyModel::updateSongList( QList< Song * > * songlist )
{
beginResetModel();
mpSonglist = songlist;
endResetModel();
}
To copy to clipboard, switch view to plain text mode
This mechanism has the downside that any currently selected entries in the model are cleared and the view is completely updated. The upside is that you are able to share the underlying song list among different models (say, a table view vs. a tree view) without duplicating the QList< Song * > and without the need to keep the copies of the QList stored in all the models in sync with each other.
I would probably do it the first way, by keeping duplicate lists if there are multiple models. It isn't so hard to keep them in sync if you have a central set of methods that handle it.
Bookmarks