PDA

View Full Version : RemoveRow problem



nategoofs
18th August 2007, 09:45
Im having some trouble with the QAbstractItemModel's remove row function working with a QTableView re-implementation. I call the function with the index from a contextMenuEvent and delete it accordingly:


void WTrackTableView :: contextMenuEvent(QContextMenuEvent * event)
{
index = indexAt(event->pos());
m_pTrackInfoObject = m_pTable->m_pTrackCollection->getTrack(index.row()+1);
if(index.isValid())
{
QMenu menu(this);
menu.addAction(PlayQueueAct);
menu.addAction(Player1Act);
menu.addAction(Player2Act);
menu.addAction(RemoveAct);
menu.exec(event->globalPos());

}
}

void WTrackTableView :: slotRemoveFromPlaylist()
{
qDebug("%i",index.row());
m_pTable->removeRow(index.row(),index);
}

index is a QModelIndex created in the header file so I can use it across functions. now, my problem is that if I select an item anywhere in the view the item still stays in the table but the item at the end of the table is removed. why would this be happening? The qDebug that I have returns the correct reference to the row that is selected, but nothing happens at that index.

jpn
18th August 2007, 13:06
May we see the removeRows() implementation of the model? Notice that the second parameter of QAbstractItemModel::removeRow() is parent of item being removed so for a flat model it should be an invalid index.

nategoofs
18th August 2007, 13:29
there is no removeRows implementation, that is using the the
bool QAbstractItemModel::removeRow ( int row, const QModelIndex & parent = QModelIndex() ) call.

jpn
18th August 2007, 13:49
QAbstractItemModel::removeRow() is a convenience function that further calls QAbstractItemModel::removeRows(). QAbstractItemModel::removeRows(), on the other hand, does nothing but returns false. You should provide a proper re-implementation for QAbstractItemModel::removeRows(). This includes calling QAbstractItemModel::beginRemoveRows(), modifying the internal data structure, and then calling QAbstractItemModel::endRemoveRows(). This ensures that the view gets updated correctly.

jpn
18th August 2007, 14:11
This is for example how QStringListModel implements removeRows():


bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
return false;

beginRemoveRows(QModelIndex(), row, row + count - 1);

for (int r = 0; r < count; ++r)
lst.removeAt(row);

endRemoveRows();

return true;
}

Here "lst" is the internal data structure, a QStringList.