Results 1 to 6 of 6

Thread: Simple DOM Model - making it editable - inserting rows

  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 Simple DOM Model - making it editable - inserting rows

    I've been messing with the Simple DOM Model Example and I want to make everything editable. I have a problem with adding rows to the model.

    Specifically, when I use insertAfter function in QDomNode:: parentNode(), the model gets updated only internally. When I collapse and expand the parent, the added node(s) are copies of the last nodes and are inserted in the end.

    The only way I am able to get it right is save XML to QString and load it again into QDomModel and QTreeView. Then everything that has been inserted is where it should be. But then all the expanded states are lost! Not very user-friendly...

    I have tried adding rows using insertRows function in QDomModel, however a copy of the entire model gets inserted as a child instead of an empty row!

    Qt Code:
    1. // Get current index
    2. QModelIndex currentTreeIdx = ui->treeView->selectionModel()->currentIndex();
    3. // Get the node corresponding to that index
    4. DomItem *itemRef = static_cast<DomItem*>(currentTreeIdx.internalPointer());
    5.  
    6. QDomElement newParentTag;
    7. QDomElement newTextTag;
    8. QDomText newText;
    9. // Create tags
    10. newParentTag = model->domDocument.createElement("Parent");
    11. newTextTag = model->domDocument.createElement("Child");
    12. newText = model->domDocument.createTextNode("Child text data");
    13. newTextTag.appendChild(newText);
    14. newSubtitleTag.appendChild(newTextTag);
    15. // Attempt to insert a new tag after the currently selected item
    16. itemRef->node().parentNode().insertAfter(newParentTag, itemRef->node());
    To copy to clipboard, switch view to plain text mode 

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


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple DOM Model - making it editable - inserting rows

    If you want the model to be updated, you need to add rows through the model API or at least inform the model how you are changing the underlying data structure.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:


  5. #3
    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: Simple DOM Model - making it editable - inserting rows

    I've tried to inform the model by calling
    Qt Code:
    1. emit dataChanged(idx, idx);
    To copy to clipboard, switch view to plain text mode 
    in DomModel : QAbstractItemModel when QModelIndex idx is updated. It seems it does nothing.

    I've even tried to call it for each index in the model.

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


  7. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple DOM Model - making it editable - inserting rows

    dataChanged() is for modifying existing elements, not for adding new ones.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following 2 users say thank you to wysota for this useful post:

    Wolf900 (29th February 2012)

  9. #5
    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: Simple DOM Model - making it editable - inserting rows

    Ok.

    Now instead of insertAfter(...), I'm trying this:

    Qt Code:
    1. model->insertRows(itemRef->row(), 1, currentTreeIdx.parent());
    To copy to clipboard, switch view to plain text mode 

    and the function being called is:

    Qt Code:
    1. bool DomModel::insertRows(int position, int rows, const QModelIndex &parent){
    2.  
    3. //DomItem *parentItem = getItem(parent);
    4. DomItem *parentItem = static_cast<DomItem*>(parent.internalPointer()); // reference item
    5.  
    6. bool success;
    7. //emit dataChanged(parent, parent);
    8.  
    9. beginInsertRows(parent, position, position + rows - 1);
    10. success = parentItem->insertChildren(position, rows, 4);
    11. endInsertRows();
    12.  
    13. return success;
    14. }
    To copy to clipboard, switch view to plain text mode 
    This one should create a row. It does create an invisible parent that I can click on to expand. But this parent now contains a copy of the entire model...


    Added after 13 minutes:


    Basically, to add to this, what I'm trying to achieve is combine "Editable Tree Model" and "Simple Dom Model" examples. So I'm having the latter as a base and I've copied the edit functions over there, changed the flags, setData, etc.
    Last edited by Wolf900; 29th February 2012 at 15:48.

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


  11. #6
    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: Simple DOM Model - making it editable - inserting rows

    It seems the culprit is the insertChildren function:

    Qt Code:
    1. bool DomItem::insertChildren(int position, int count, int columns){
    2. if (position < 0 || position > childItems.size()){
    3. return false;
    4. }
    5.  
    6. for (int row = 0; row < count; row++){
    7. DomItem *item = new DomItem(*(new QDomNode()), row, this); // this doesn't seem to be correct...
    8. childItems.insert(position, item);
    9. }
    10.  
    11. return true;
    12. }
    To copy to clipboard, switch view to plain text mode 

    What insertRows does now is replace the selected row with an empty row.

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


Similar Threads

  1. Making a QTableWidget Editable
    By llemes4011 in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2010, 19:12
  2. trouble making simple DOM model example editable
    By Mamra in forum Qt Programming
    Replies: 6
    Last Post: 1st October 2009, 14:40
  3. Efficient way of inserting rows?
    By afflictedd2 in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2008, 20:01
  4. QTreeView rows editable
    By Pesho in forum Qt Programming
    Replies: 5
    Last Post: 13th September 2007, 13:19
  5. QTreeWidget - Making a Column editable.
    By Preeteesh in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2007, 12:02

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.