I use it for a 2 level TreeModel. The second level has another data type, so I have another set of methods like addSubItem(ItemData *data, SubItemData *subdata) and removeSubItem(ItemData *data, SubItemData *subdata). These methods query for the TreeItem which contains data and add a TreeItem child with subdata. But for testing purposes, I removed the code for the second level, thus am currently only working with the first level to solve this problem.
@Christian-Ehrlicher said in QTreeView crashes due to deleted row:
Then please remove layoutChanged() - this is not needed at all.
If I remove that, the QTreeView does not show the added item.
@Christian-Ehrlicher said in QTreeView crashes due to deleted row:
Also you don't check if getItem() returns a valid pointer.
getItem() looks like this (removed the Custom for simplicity):
TreeItem *TreeModel::getItem(const QModelIndex &index) const { if (index.isValid()) { TreeItem *item = static_cast<TreeItem*>( index.internalPointer() ); if (item) { return item; } } return _rootItem; }So, it should always return a valid pointer, or am I missing something here?
@Christian-Ehrlicher said in QTreeView crashes due to deleted row:
provide a minimal, compilable example of the model. We e.g. don't know what's happening inside _rootItem->removeChild().
The implementations for the TreeItem look like this (_childItems is a QList<TreeItem*>):
void TreeItem::appendChild(TreeItem *item) { item->_parentItem = this; _childItems.append( item ); } void TreeItem::removeChild(TreeItem *item) { _childItems.removeOne( item ); } int TreeItem::row() const { if( _parentItem != nullptr ) { return _parentItem->_childItems.indexOf( const_cast<TreeItem*>( this ) ); } return 0; }The TreeModel is mainly implemented accoring to the example EditableTreeModel. Please let me know, if you need more clarification about the model.
The last portion of the stack trace looks like this:
1 TreeItem::row treeitem.cpp 157 0x7ff7a780c808
2 TreeModel::parent treemodel.cpp 265 0x7ff7a780d8a2
3 QModelIndex::parent qabstractitemmodel.h 481 0x7ffe8a33a63a
4 QTreeView::isIndexHidden qtreeview.cpp 3073 0x7ffe8a33a63a
5 QTreeView::isIndexHidden qtreeview.cpp 3071 0x7ffe8a33a63a
6 QTreeViewPrivate::visualRect qtreeview.cpp 1098 0x7ffe8a33b837
7 QTreeView::visualRect qtreeview.cpp 1073 0x7ffe8a33ba08
8 QAbstractItemView::inputMethodQuery qabstractitemview.cpp 906 0x7ffe8a2de7a0
This is because I override the dragMoveEvent(QDragMoveEvent *event), dropEvent(QDropEvent *event) and paintEvent(QPaintEvent *event) in my subclassed QTreeView.