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. Crash when building XML QDomElement in a method (probably just my bad C++)

Crash when building XML QDomElement in a method (probably just my bad C++)

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.0k 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.
  • B Offline
    B Offline
    Batox
    wrote on last edited by
    #1

    Using MSVC 2010, Win 7 (64), Qt 4.8.4. I'm pretty sure I just got some parameter types wrong, but I just fail to see which... ? I'd also be grateful for hints to better solutions of this problem (teaching a class how to write itself to an XML file).

    @#include <QtXML>

    class base {
    public:
    virtual QDomElement& Write(QDomDocument& doc) {
    QDomElement& de = doc.createElement("Element1");
    de.setAttribute("Attr1", "Value1");
    return de;
    }
    };

    class derived : public base {
    public:
    QDomElement& Write(QDomDocument& doc) {
    QDomElement& de = base::Write(doc);
    de.setAttribute("Attr2", 2);
    return de;
    }
    };

    int main(int argc, char *argv[])
    {
    QDomDocument doc("testtype");
    QDomElement root = doc.createElement("test");
    doc.appendChild(root);
    //#define crash 1
    #ifdef crash
    // IMO using these two classes should result in
    // the exact same calls as the positive case below
    base t1;
    derived t2;

    root.appendChild(t1.Write(doc));
    root.appendChild(t2.Write(doc));
    #else
    // This is the positive case - no classes involved
    QDomElement& de = doc.createElement("Element1");
    de.setAttribute("Attr1", "Value1");
    root.appendChild(de);
    de = doc.createElement("Element1");
    de.setAttribute("Attr1", "Value1");
    de.setAttribute("Attr2", 2);
    root.appendChild(de);
    #endif
    QFile of("test.xml");
    if (!of.open(QIODevice::WriteOnly))
    return 1;
    QTextStream ts(&of);
    ts << doc.toString();
    of.close();
    return 0;
    }
    @

    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