PDA

View Full Version : Crash in editorEvent with a delete key press event



xiangyu
26th March 2013, 15:57
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.



bool Delegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){

TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
DisplayProperty* property = item->displayProperty();
TreeModel * treeModel = (TreeModel*)(model);

/////////////////right button press action//////////////////////////
if (event->type()==QEvent::MouseButtonPress){
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if((mouseEvent->button() == Qt::RightButton)&&(index.column()==0)){
qDebug()<<"right button delete";
return deleteListItem(index, treeModel);
}
}

/////////////////delete key press action//////////////////////////
if (event->type()==QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
Qt::Key key = (Qt::Key)keyEvent->key();
if (key == Qt::Key_Delete){
qDebug()<<"delete key delete";
return deleteListItem(index, treeModel);
}
}
return false;}

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?

THANKS!