In QTreeView, it crashes when delete the last child
-
Env
Linux x86-64
Qt 6.2.0Problem
Settings for QTreeView:
QAbstractItemView::SelectRows
QAbstractItemView::SingleSelection
When deleting a item that's not the last child, the current index is automatically move to the next sibling child.
When deleting a item that is the last child, it crashes.If call
selectionModel()->clearCurrentIndex()
before deleting, it's successful to delete the last child. But the currentIndex is moved to the top left index, which is not friendly to users, any better way? e.g. move current index to next smartly (i.e. might be with a diffrent parent) -
Solved by adding the two lines of codes in
MyModel::index
, it works perfectly.QModelIndex MyModel::index(int row, int column, const QModelIndex & parent) const { if (!hasIndex(row, column, parent)) return QModelIndex(); ... }
BTW, without the two lines of codes, everything works well except deleting the last child.
I think it's a little hard to find out.Thanks
-
Please provide a minimal, compilable example so we can see how you delete your items and if you do something wrong.
-
@Christian-Ehrlicher
When testing on "Simple Tree Model Example",
After currentIndex is deleted, it is smart enough to find an existing item as currentIndex even with a different parent. -
Hi,
Are you using a custom model ?
-
Did you run it through the model tester ?
-
Yes you should. The base class implementation does nothing. Only you know how to remove the data that makes your model.
-
Solved by adding the two lines of codes in
MyModel::index
, it works perfectly.QModelIndex MyModel::index(int row, int column, const QModelIndex & parent) const { if (!hasIndex(row, column, parent)) return QModelIndex(); ... }
BTW, without the two lines of codes, everything works well except deleting the last child.
I think it's a little hard to find out.Thanks
-
You should check your model with the model tester: https://wiki.qt.io/Model_Test