How does the QML Treeview data model implement add, delete, or modify methods in C++?
-
Hi,
Yes they are. The methods overloaded in the example do not need to call them. But they will be when you remove rows from your model for example.
-
Hi,
Yes they are. The methods overloaded in the example do not need to call them. But they will be when you remove rows from your model for example.
wrote on 31 Aug 2020, 02:24 last edited by@SGaist Is there anything wrong with my add and delete operation?
void TreeModel::appendChild(const QModelIndex& index) { TreeItem* clickItem = static_cast<TreeItem*>(index.internalPointer()); /*beginRemoveRows(index, 0, 0); clickItem->deleteAllChild(); endRemoveRows();*/ removeRows(0, clickItem->childCount(), index); QList<QVariant> TestItem; TestItem.append("TestItem"); TestItem.append("TI"); TreeItem* TestItem_Item = new TreeItem(TestItem, clickItem); QList<QVariant> TestItem2; TestItem2.append("TestItem2"); TestItem2.append("TI2"); TreeItem* TestItem_Item2 = new TreeItem(TestItem2, TestItem_Item); beginInsertRows(index, 0, 0); TestItem_Item->appendChild(TestItem_Item2); clickItem->appendChild(TestItem_Item); endInsertRows(); } bool TreeModel::removeRows(int row, int count, QModelIndex parent) { if (parent.isValid()) { for (int r = row; r < (row + count); ++r) { QModelIndex idxRemove = parent.child(r, 0); TreeItem* fiRemove = static_cast<TreeItem*>(idxRemove.internalPointer()); if (idxRemove.child(0, 0).isValid()) { bool childRemoved = removeRows(0, fiRemove->childCount(), idxRemove); } } TreeItem* fiParent = static_cast<TreeItem*>(parent.internalPointer()); int last = 0; if (row + count - 1 > 0) { last = row + count - 1; } beginRemoveRows(parent, row, last); fiParent->deleteAllChild(); endRemoveRows(); return true; } else { for (int r = row; r < (row + count); ++r) { TreeItem* slnItem = m_rootItem->child(r); bool projectRemoved = removeRows(0, slnItem->childCount(), index(r, 0, QModelIndex())); } if ((row + count) > 0) { beginRemoveRows(QModelIndex(), row, row + count - 1); m_rootItem->deleteAllChild(); endRemoveRows(); } return true; } return false; }
-
What am I supposed to look for ? What is the issue ?
-
Again: what issue do you have with your current code ?
-
Sorry but you are really unclear...
-
As I already asked you: what is wrong with the methods you wrote ?
-
wrote on 3 Sept 2020, 22:13 last edited by
@mirro What do you mean by a 'modified method'?
All I can think is that you want an example of using the
dataChanged
signal. You would use this if you are modifying the data associated with a tree node (in contrast with your add and remove methods that are concerned with structural changes to the tree) but the details of that are mainly specific to your application - e.g. what data you want to change, how the GUI presentation implemented in your use of theTreeView
depends on that data, etc.The basic point is that if you are changing data associated with a node that the TreeView needs to know about then emit
dataChanged
for the node. -
@mirro What do you mean by a 'modified method'?
All I can think is that you want an example of using the
dataChanged
signal. You would use this if you are modifying the data associated with a tree node (in contrast with your add and remove methods that are concerned with structural changes to the tree) but the details of that are mainly specific to your application - e.g. what data you want to change, how the GUI presentation implemented in your use of theTreeView
depends on that data, etc.The basic point is that if you are changing data associated with a node that the TreeView needs to know about then emit
dataChanged
for the node.wrote on 4 Sept 2020, 08:52 last edited by@Bob64 Is this the right way to implement the substitution?
bool TreeModel::replaceRows(int row, int count, QModelIndex parent) { if (parent.isValid()) { QModelIndex idxRemove = parent.child(row, 0); TreeItem* fiModify = static_cast<TreeItem*>(idxRemove.internalPointer()); if (fiModify.child(0, 0).isValid()) { beginRemoveRows(QModelIndex(),row,0); removeRows(0, 0, index(row, 0, QModelIndex())); endRemoveRows(); QList<QVariant> TestItem; TestItem.append("TestItem"); TreeItem* TestItem_Item = new TreeItem(TestItem); rootItem->replace(row,TestItem_Item); dataChanged(index(row,0),index(row,0),{NameEnum}); return true; } } else { beginRemoveRows(QModelIndex(), row, 0); removeRows(row, 0, index(row, 0, QModelIndex())); endRemoveRows(); QList<QVariant> TestItem; TestItem.append("TestItem"); TreeItem* TestItem_Item = new TreeItem(TestItem); rootItem->replace(row,TestItem_Item); dataChanged(index(row,0),index(row,0),{NameEnum}); return true; } return false; }
-
@Bob64 Is this the right way to implement the substitution?
bool TreeModel::replaceRows(int row, int count, QModelIndex parent) { if (parent.isValid()) { QModelIndex idxRemove = parent.child(row, 0); TreeItem* fiModify = static_cast<TreeItem*>(idxRemove.internalPointer()); if (fiModify.child(0, 0).isValid()) { beginRemoveRows(QModelIndex(),row,0); removeRows(0, 0, index(row, 0, QModelIndex())); endRemoveRows(); QList<QVariant> TestItem; TestItem.append("TestItem"); TreeItem* TestItem_Item = new TreeItem(TestItem); rootItem->replace(row,TestItem_Item); dataChanged(index(row,0),index(row,0),{NameEnum}); return true; } } else { beginRemoveRows(QModelIndex(), row, 0); removeRows(row, 0, index(row, 0, QModelIndex())); endRemoveRows(); QList<QVariant> TestItem; TestItem.append("TestItem"); TreeItem* TestItem_Item = new TreeItem(TestItem); rootItem->replace(row,TestItem_Item); dataChanged(index(row,0),index(row,0),{NameEnum}); return true; } return false; }
wrote on 4 Sept 2020, 10:18 last edited by@mirro Honestly, I don't know. I don't understand exactly what your method is intended to do. I can see that you are removing rows there but you aren't using the parent index. No rows are being added, so I don't know where the 'replacement' aspect comes in. And then I don't understand the intent of the following code that calls
replace
on yourTreeItem
and emitsdataChanged
.I am not an expert user, but when I implemented a tree model, I kept structural changes to the tree such as adding and removing rows completely separate from updating data at a node (which is where
dataChanged
is needed). -
@mirro Honestly, I don't know. I don't understand exactly what your method is intended to do. I can see that you are removing rows there but you aren't using the parent index. No rows are being added, so I don't know where the 'replacement' aspect comes in. And then I don't understand the intent of the following code that calls
replace
on yourTreeItem
and emitsdataChanged
.I am not an expert user, but when I implemented a tree model, I kept structural changes to the tree such as adding and removing rows completely separate from updating data at a node (which is where
dataChanged
is needed). -
That's your model. You are supposed to know how it works therefore how you should manage the data it handles.
11/15