Results 1 to 2 of 2

Thread: QTreeView editable DOM model

  1. #1
    Join Date
    Nov 2011
    Posts
    11
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Unhappy QTreeView editable DOM model

    Hello,

    I have a QTreeView to which I set a subclassed DomModel:QAbstractItemModel. Each item is a DomItem which deals mostly with QDomNode. I set QDomDocument to this model. I think I've derived this system from one of the Qt examples.

    It has 3 columns: 0 for node name, 1 for attributes and 2 for value.

    Anyway, I wanted to make this XML DOM tree editable. I've modified some flags such as Qt::ItemIsEditable and some other things in the model class and now I can edit the model through QTreeView by double clicking.

    For column 2 it's easy, since QDomItem has this setNodeValue function, however I've found there are no "set" functions for item->node().NodeName() and item->node().attributes() which would, I presume, modify columns 0 and 1.

    So now when I modify column 2 it works, however columns 0 and 1 revert to their previous values upon pressing enter. But I cannot seem to find any way to modify them.

    Qt Code:
    1. bool DomModel::setData(const QModelIndex &index, const QVariant &value,
    2. int role)
    3. {
    4. if (role != Qt::EditRole) return false;
    5.  
    6. DomItem *item = static_cast<DomItem*>(index.internalPointer());
    7.  
    8. switch (index.column()){
    9. case 0:
    10. // ???
    11. break;
    12. case 1:
    13. // ???
    14. break;
    15. case 2:
    16. item->node().setNodeValue(value.toString()); // This works - QTreeView's column 2 is updated
    17. break;
    18. }
    19. ...
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2011
    Posts
    11
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default Re: QTreeView editable DOM model

    Well, apparently item->node().toElement() which returns a QDomElement has the necessary "set" functions and it works. So I think I've found a way to fully modify my XML DOM file through QTreeView.

    This does the trick for me:
    Qt Code:
    1. switch (index.column()){
    2. case 0: // added - works
    3. item->node().toElement().setTagName(value.toString());
    4. break;
    5. case 1: // added - works
    6. // this shall be modified to account for nonstandard spacings, etc.
    7. aux = value.toString().trimmed();
    8. aux.remove("\"");
    9. attributes.clear();
    10. attributes = aux.split(" ");
    11. for(int i = 0; i<attributes.size(); i++){
    12. item->node().toElement().setAttribute(attributes.at(i).split("=").at(0),
    13. attributes.at(i).split("=").at(1));
    14. }
    15. break;
    16. case 2: // Left it as it is
    17. item->node().setNodeValue(value.toString()); // This works - QTreeView is updated
    18. break;
    19. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Wolf900 for this useful post:

    qtCuckoo (16th April 2012)

Similar Threads

  1. Replies: 2
    Last Post: 21st April 2010, 11:37
  2. Editable QTreeView
    By winston2020 in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2009, 13:18
  3. Editable Tree Model
    By kloffy in forum Qt Programming
    Replies: 5
    Last Post: 10th October 2007, 15:49
  4. QTreeView rows editable
    By Pesho in forum Qt Programming
    Replies: 5
    Last Post: 13th September 2007, 13:19
  5. Setting one* column of QTreeView to be editable
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2006, 20:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.