I have a Treemodel, a QStyledItemDelegate and a QTreeview in my code.

In the QStyledItemDelegate, I have overwrited the methode editorEvent() to delete the item in the TreeModel.

Qt Code:
  1. bool Delegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){
  3. TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
  4. DisplayProperty* property = item->displayProperty();
  5. TreeModel * treeModel = (TreeModel*)(model);
  7. /////////////////right button press action//////////////////////////
  8. if (event->type()==QEvent::MouseButtonPress){
  9. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
  10. if((mouseEvent->button() == Qt::RightButton)&&(index.column()==0)){
  11. qDebug()<<"right button delete";
  12. return deleteListItem(index, treeModel);
  13. }
  14. }
  16. /////////////////delete key press action//////////////////////////
  17. if (event->type()==QEvent::KeyPress){
  18. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
  19. Qt::Key key = (Qt::Key)keyEvent->key();
  20. if (key == Qt::Key_Delete){
  21. qDebug()<<"delete key delete";
  22. return deleteListItem(index, treeModel);
  23. }
  24. }
  25. return false;}
To copy to clipboard, switch view to plain text mode 

The methode handle well the right click event, but not for the delete_Key press event. it crashs often. So I suppose it will not be a problem of function deleteListItem().

I have impression, the delegate will do some update of view after the execution of editorEvent, and as I have delete the index item, it can't find anymore the item of this index. So it crash.

Can someone tell me why and give me some sugesstion of delete item using the delete key in treeModel?