Treeview remove row
-
The first step in diagnosing a model is passing it through the model test. It usually tells you what you are doing wrong
-
Qt 5.11 or later
- Add the QtTest module to your pro file like: QT += testlib
How do I add this module in visual studio? I am not using qt creator and when i try to #include <QAbstractItemModelTester> i get this error "cannot open source file QAbstractItemModelTester "
- Add the QtTest module to your pro file like: QT += testlib
-
Thank you very much, sir. Now I need to debug the errors. I don't get it, beside I use a unique_ptr to store nodes, the model and tree nodes are the same as in the qt tree model example.. Maybe someone has encountered this errors
-
Use the debugger to see what problems are being reported.
The first screenshot is telling you that either yourindex()
orparent()
method are wrong (it checks thatmodel->parent(model->index(row,column,idx))==idx
The second screenshot is telling you your
index()
method is wrong. Passing a row, column and parent within therowCount(parent)
andcolumnCount(parent)
constraints returns an invalid index. This is likely caused by you ignoring theparent
argument inTreeModel::columnCount
-
Sir, do you have any link about qt debug in visual studio?
-
@AlexandruToma said in Treeview remove row:
Sir, do you have any link about qt debug in visual studio?
Simply debug in Visual Studio. There is no such thing as "qt debug".
-
I got the same problem as in this topic.
I changed columnCount function but the errors are still there
int TreeModel::columnCount(const QModelIndex &parent) const { return (!parent.isValid() || parent.column() == 0) ? rootItem->columnCount() : 0; }
I run tests on editable tree model in qt creator and I got the same error like in my project.
Here is the error.
// Common error test #3, the second column should NOT have the same children // as the first column in a row. // Usually the second column shouldn't have children. if (model->hasIndex(0, 1)) { QModelIndex topIndex1 = model->index(0, 1, QModelIndex()); MODELTESTER_VERIFY(topIndex1.isValid()); if (model->rowCount(topIndex) > 0 && model->rowCount(topIndex1) > 0) { QModelIndex childIndex = model->index(0, 0, topIndex); MODELTESTER_VERIFY(childIndex.isValid()); ***QModelIndex childIndex1 = model->index(0, 0, topIndex1);*** MODELTESTER_VERIFY(childIndex1.isValid()); MODELTESTER_VERIFY(childIndex != childIndex1); } }
-
You are right, there is a bug in the example. I opened a ticket: https://bugreports.qt.io/browse/QTBUG-92178 the correct implementation should be:
int TreeModel::rowCount(const QModelIndex &parent) const { if(parent.isValid() && parent.column()>0) return 0; const TreeItem *parentItem = getItem(parent); return parentItem ? parentItem->childCount() : 0; }
-
@VRonin said in Treeview remove row:
if(parent.isValid() && parent.column()>0) return 0; const TreeItem *parentItem = getItem(parent); return parentItem ? parentItem->childCount() : 0;
All good now. Thank you very much, sir.