Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. DOM classes: Question about the design
Qt 6.12 LTS is out! See what's new in the release blog

DOM classes: Question about the design

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.7k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I have a question regarding the class design of the Qt DOM classes:

    Usually I create a document like this:

    @QDomDocument doc;
    QDomElement root = doc.createElement("test");
    doc.appendChild(root);
    QDomElement testElement = doc.createElement("second_test");
    root.appendChild(testElement);@

    It is a little annoying having to call the appendChild method every time after I create an element. Why is the element not added to the parent element or the doc when it is created? Wouldn't that make more sense?
    What is the reason for the class design the qt developers used?

    I think this would look much nicer:

    @QDomDocument doc;
    QDomElement root = doc.createElement("test");
    QDomElement testElement = root.createElement("second_test");
    @

    Thanks,
    marsupial

    1 Reply Last reply
    0
    • I Offline
      I Offline
      iunknwn
      wrote on last edited by
      #2

      Your proposal duplicates functionlity in two different classes.
      In QDomDocument and in QDomElement. If gone with your approach, it'd further require duplication in QDomComment, QDomCDATASection, etc...

      Also, another philosophical reason is clearly mentioned in the QDomDocument that,

      "http://doc.qt.nokia.com/4.7/qdomdocument.html#details":http://doc.qt.nokia.com/4.7/qdomdocument.html#details

      bq. Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document, the document class also contains the factory functions needed to create these objects. The node objects created have an ownerDocument() function which associates them with the document within whose context they were created.

      Make yourself a little helper class based on below pseudo.

      @ctor(){
      _doc = new QDomDocument();
      }

      ~(){
      delete _doc;
      }

      //Type is enum.
      QDomNode* class::createNode(QDomNode* parent, const QString& nodeText, Type type)
      {

      switch(type){
          case Element:
               QDomElement* elem = _doc->createElement(nodeText);
               parent->appendChild(*elem);
               return elem;
          ...
      }
      

      }@

      Vista x64 with Qt 4.8.2 and VS2010

      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
      • Unsolved