Solved How does the QML Treeview data model implement add, delete, or modify methods in C++?
-
@SGaist
Excuse me, how to write the modification method?Can you provide some sample code? -
Again: what issue do you have with your current code ?
-
There is currently no modified method. can you?
-
Sorry but you are really unclear...
-
@SGaist You must know how to write the modify method , right?
Help me write an example. -
As I already asked you: what is wrong with the methods you wrote ?
-
@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. -
@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; }
-
@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). -
@Bob64 Hi, How do you replace node data? Can you sample the code.
-
That's your model. You are supposed to know how it works therefore how you should manage the data it handles.