Treeview remove row
-
wrote on 23 Mar 2021, 23:12 last edited by
The first step in diagnosing a model is passing it through the model test. It usually tells you what you are doing wrong
-
wrote on 24 Mar 2021, 07:54 last edited by
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
-
wrote on 24 Mar 2021, 09:02 last edited by
I assume you have the Qt Visual Studio extension installed, if so right click on your project, go to qt project settings and, in the modules tab, tick Qt Test (remember to remove it once you are done)
-
wrote on 24 Mar 2021, 13:17 last edited by AlexandruToma
-
wrote on 24 Mar 2021, 13:52 last edited by VRonin
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
-
wrote on 25 Mar 2021, 07:48 last edited by
Sir, do you have any link about qt debug in visual studio?
-
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".
-
wrote on 25 Mar 2021, 21:45 last edited by
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); } }
-
wrote on 26 Mar 2021, 12:38 last edited by VRonin
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; }
-
wrote on 27 Mar 2021, 11:44 last edited by
@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.
11/11