Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple DOM Model - making it editable - inserting rows

Simple DOM Model - making it editable - inserting rows

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wolf-S
    wrote on last edited by
    #1

    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!

    @
    // Get current index
    QModelIndex currentTreeIdx = ui->treeView->selectionModel()->currentIndex();
    // Get the node corresponding to that index
    DomItem itemRef = static_cast<DomItem>(currentTreeIdx.internalPointer());

    QDomElement newParentTag;
    QDomElement newTextTag;
    QDomText newText;
    // Create tags
    newParentTag = model->domDocument.createElement("Parent");
    newTextTag = model->domDocument.createElement("Child");
    newText = model->domDocument.createTextNode("Child text data");
    newTextTag.appendChild(newText);
    newSubtitleTag.appendChild(newTextTag);
    // Attempt to insert a new tag after the currently selected item
    itemRef->node()removed.insertAfter(newParentTag, itemRef->node());
    @

    I've tried to inform the model by calling
    @emit dataChanged(idx, idx);@

    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.

    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.

    1 Reply Last reply
    0
    • W Offline
      W Offline
      Wolf-S
      wrote on last edited by
      #2

      It seems I have a problem in the insertChildren function. As it is now, it replaces the selected row with an empty row.

      @
      bool DomItem::insertChildren(int position, int count, int columns){
      if (position < 0 || position > childItems.size()){
      return false;
      }

      for (int row = 0; row < count; row++){
          DomItem *item = new DomItem(*(new QDomNode()), row, this);   // this doesn't seem to be correct...
          childItems.insert(position, item);
      }
      
      return true;
      

      }
      @

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ivw1
        wrote on last edited by
        #3

        Hi Wolf-S,

        were you able to solve the issue?

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wolf-S
          wrote on last edited by
          #4

          I've used
          @item->node().toElement() @

          method that has the necessary "set" functions to modify the table. Refer to "my answer on stackoverflow":http://stackoverflow.com/questions/7998843/qt-qtreeview-editable-dom-model/8054115#8054115

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ivw1
            wrote on last edited by
            #5

            Thank you for pointing out stackoverflow. Are you also able to insert or remove nodes?

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Wolf-S
              wrote on last edited by
              #6

              I've dug up some old code, I think it's what you need for removing but I might be wrong. Most of the things were taken from the standard Qt4 class examples.

              DomModel is a subclass of QAbstractItemModel
              DomItem is a separate class containing QDomNode

              @
              DomModel::deleteIndex(const QModelIndex &index){
              DomItem item = static_cast<DomItem>(index.internalPointer());
              DomItem parent_item = static_cast<DomItem>(index.parent().internalPointer());
              QDomNode node = item->node();
              QDomNode parent_node = parent_item->node();
              parent_node.toElement().removeChild(node);
              return true;
              }@
              and it's called by
              @
              QModelIndex index = ui->treeView->currentIndex(); // treeView is a subclass of QTreeView
              model->deleteIndex(index); // DomModel *model;
              @

              And inserting is done using the methods (currentTreeIdx is a variable of QModelIndex type)
              @
              DomItem itemRef = static_cast<DomItem>(currentTreeIdx.internalPointer()); // reference item

                      newMainTag = model->domDocument.createElement("Main");
                      newTextTag =  model->domDocument.createElement("Text");
                      newText = model->domDocument.createTextNode("***New Text Data***");  // Add text data itself
                      newTextTag.appendChild(newText);
                      newMainTag.appendChild(newTextTag);
                      itemRef->node().toElement().appendChild(newMainTag);
              

              @

              I remember inserting was still buggy, I couldn't keep the cursor in the same place after inserting.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved