PDA

View Full Version : Media Player Example - removeMedia



Festus Hagen
15th September 2013, 05:43
Hi all,

Kinda figure someone would have already done/asked this ... I cannot find it if they have!

Learning and Tinkering with the Media Player Example (http://qt-project.org/doc/qt-5.0/qtmultimedia/multimediawidgets-player.html).
My goal is learning the underlying PlaylistModel.

Trying to add the ability to remove items from the playlist, Added:


void Player::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Delete:
if(playlistView->hasFocus())
removeFromPlaylist();
break;
default:
event->ignore();
break;
}
}

And


void Player::removeFromPlaylist()
{
QModelIndex idx = playlistView->currentIndex();
if(!idx.isValid())
return;
playlist->removeMedia(idx.row());
}


The item is removed however there are errors if within four items of the end of list.
The errors are:


QAbstractItemModel::endInsertRows: Invalid index ( 4 , 0 ) in model PlaylistModel(0x1a676848)

Of course the numbers change ....

If the last item is always the item that is removed it works fine, It appears to be trying to select the row +3 after deletion and I cannot figure out why.
Any ideas/suggestions/fixes ???

-Enjoy
fh : )_~

fant
1st June 2014, 16:52
Hi Festus,
I know, it's a little bit late, but recently I had this problem, too. Here is the solution:

In playlistmodel.cpp you has to change the endRemoveItems method. This method should call endRemoveRows, not endInsertRows.
Then you can overwrite removeRow with public access modifier:



//in header file the default value of parent parameter is a new instance of QModelIndex()

bool PlaylistModel::removeRow(int row, const QModelIndex &parent){

if(0 > row || (row + 1) > rowCount(parent))
return false;

beginRemoveRows(parent, row, row);

foreach(QModelIndex index, _data.keys())
if(row == index.row()){
_data.remove(index);
break;
}

endRemoveRows();

return true;
}