Results 1 to 3 of 3

Thread: Problems writting a svg file

  1. #1
    Join Date
    Feb 2010
    Location
    Cuba
    Posts
    35
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Problems writting a svg file

    I need to change some element attributes of a xml file. I've been trying to achieve this using QXmlStreamWriter class, but it does not work fine, because when I call the function, all elements are deleted of the xml file, except the changed attribute.
    For example, I want to change the width of a rectangle, in a SVG file, keeping the other elements and attributes intact.
    Here is my code:

    Qt Code:
    1. void Editor::changeElementAttribute(int pos, QVariant newData)
    2. {
    3. DomItem *item = static_cast<DomItem*>(treeView->currentIndex().internalPointer());
    4. QDomNode node = item->node();
    5. QDomNamedNodeMap attributeMap = node.attributes();
    6.  
    7. file.setFileName(currentPath);
    8.  
    9. if (!file.open(QFile::WriteOnly | QFile::Text))
    10. {
    11. QMessageBox::warning(new QWidget(), tr("SVG Editor"),
    12. tr("Cannot write file %1:\n%2.").arg(currentPath)
    13. .arg(file.errorString()));
    14. return;
    15. }
    16.  
    17. stream.setDevice(&file);
    18. stream.writeStartElement(node.nodeName()); //tagname
    19. stream.writeAttribute(attributeMap.item(pos).nodeName(), newData.toString()); //attribute and value
    20. stream.writeEndElement();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Any help is welcome.
    Thanks in advanced!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,320
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems writting a svg file

    Your code is doing exactly what you told it to do: Open the file, erase everything in it, write out a single element tag, write a single attribute for that element, then close the element and the file. Why did you think that by doing that, it would somehow magically remember everything that was in it already? XML files are no different than any other text file, there is just a fancy API to add structure to the text.

    If you are already keeping the DOM document tree in memory (it looks like you are, since you associate DOM items with tree items), then you need to simply change the attribute in the attribute map. After the user is finished editing, then you need to rewrite the entire DOM tree back out to the file, using QDomDocument::save();

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

    Hogwarts (7th July 2011)

  4. #3
    Join Date
    Feb 2010
    Location
    Cuba
    Posts
    35
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problems writting a svg file

    Thanks a lot. I've been working a while with QDomDocument and I had not seen the save() function. I know all everything is in the assistant, but...
    Thanks a lot...I can now either save the changes.
    The code was so:
    Qt Code:
    1. void Editor::changeElementAttribute()
    2. {
    3. QFile file(currentPath);
    4. if (!file.open(QFile::WriteOnly | QFile::Text))
    5. {
    6. QMessageBox::warning(new QWidget(), tr("SVG Editor"),
    7. tr("Cannot write file %1:\n%2.").arg(currentPath)
    8. .arg(file.errorString()));
    9. return;
    10. }
    11. QTextStream out(&file);
    12. document.save(out, 2);
    13. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. downloading the file problems
    By migel in forum Newbie
    Replies: 0
    Last Post: 7th June 2011, 17:30
  2. I'm writting a C++ and Qt4 trojan with GPL license
    By paju1986 in forum Qt-based Software
    Replies: 8
    Last Post: 11th December 2010, 07:58
  3. Problems with executing the *.exe file
    By iksarp in forum Newbie
    Replies: 14
    Last Post: 28th February 2010, 05:49
  4. QDataStream-serialization not writting to file
    By john_god in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2009, 12:27
  5. Problems writing to file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2009, 16:01

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
  •  
Qt is a trademark of The Qt Company.