View Full Version : Editable Tree Model

10th October 2007, 00:25
I'm currently trying to add editing capabilities to the Simple Tree Model Example (http://doc.trolltech.com/4.1/itemviews-simpletreemodel.html) and it isn't going too well. Before I put my malfunctioning code up here, I'd like to ask whether somebody knows full featured Tree Model Example.

Edit: I'm specifically looking for a way to add/remove nodes and edit their contents. I have read the Editable Model (http://doc.trolltech.com/4.1/model-view-creating-models.html#an-editable-model) doc, but I'm still not quite sure how to implement those methods for a tree-like data structure...

10th October 2007, 01:02
You can use QStandardItemModel which supports tree structures and is editable. If you want to implement edit capabilities to the simple tree model, you'll have to implement insertRow() and removeRow() methods and make them add/remove items from the internal QList. Additionally you'll have to implement setData() to operate on the list changing its contents and make flags() return ItemIsEditable (among other values) for items you want editable.

10th October 2007, 02:30
I'd rather not use QStandardItemModel because I already have my own tree implementation. Unless I'm missing something, using QStandardItemModel would mean that I'd have to take all the nodes from my tree and put them into QStandardItems. That would result in two different tree representations of the same data.
I'm aware of the methods I need to implement, but obviously I must be doing something wrong. Anyways, I'll give it another shot tomorrow and post my results here...

10th October 2007, 10:54
Yes, you are right. If you already have a tree structure, it'd be a redundancy. If you post your code here, we'll try to help you (just please attach it instead of embedding if it's more than 50 lines of code).

10th October 2007, 16:45
Ok, I did get it to work. Heres the editable version of the simple tree model.

10th October 2007, 16:49
And here's the application to run it.

Edit: I would not advise on using this code as is. I'm just happy that the basic functionality is there and I have a pretty good understanding of how to implement an editable tree model now. If I have some free time I'll properly implement the necessary methods (e.g. to support multiple columns).