Results 1 to 8 of 8

Thread: Help With A Custom Implementation of QGraphicsSvgItem

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Help With A Custom Implementation of QGraphicsSvgItem

    So I'm trying to follow the code on this thread:

    http://www.qtcentre.org/threads/5394...raphicsSvgItem

    where the guy apparently discovered that the SVG attributes for things like color can't be changed within the QGraphicsSvgItem itself, so I need to have some kind of wrapper that holds the XML data and can edit it on the fly, then have the QSvgRenderer reload the information. The problem I am getting is that, although the renderer will load information from a file just fine, I can't get it to read from a byte array.

    Here's my code snippet:

    Qt Code:
    1.  
    2. QFile *file = new QFile(":/assets/outline.svg");
    3. file->open(QIODevice::ReadOnly);
    4. QTextStream *stream = new QTextStream(file);
    5. QString str = stream->readAll();
    6. doc->setContent(str);
    7. QByteArray barr = doc->toByteArray();
    8. file->close();
    9.  
    10. QSvgRenderer *renderer = new QSvgRenderer();
    11.  
    12. std::cout << renderer->load(QString(":/assets/outline.svg")) << std::endl; // Returns true
    13. std::cout << renderer->load(barr) << std::endl; // Returns false
    14.  
    15. item->setSharedRenderer(renderer);
    16.  
    17. scene->addItem(item);
    To copy to clipboard, switch view to plain text mode 

    (My actual code doesn't have both "renderer->load" lines in series like this. I just put them in the snippet to show what I am using.)

    As the comments show, when the renderer loads the SVG straight from the file, it loads fine and I see my graphic, but when I try and load the byte array that went through the QDomDocument, I get nothing. Does anyone have any idea as to why this might be the case?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help With A Custom Implementation of QGraphicsSvgItem

    Is the QDomDocument still ok?

    Does the content of "barr" look correct?

    Btw, create QFile on the stack or delete it manually, otherwise it leaks. No need for QTextStream, QDomDocument can read from a QIODevice (the file) directly.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help With A Custom Implementation of QGraphicsSvgItem

    Quote Originally Posted by anda_skoa View Post
    Is the QDomDocument still ok?

    Does the content of "barr" look correct?
    Yes and yes.

    Btw, create QFile on the stack or delete it manually, otherwise it leaks. No need for QTextStream, QDomDocument can read from a QIODevice (the file) directly.
    I know that about the QDomDocument. I just made an intermediary QTextStream to make 100% sure that it wasn't some error in reading the file.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help With A Custom Implementation of QGraphicsSvgItem

    Hmm.

    Can you try the third overload? The one taking a QXmlStreamReader? You should be able to initialize one with the byte array.

    If it fails as well, its error/errorString methods might provide more insight into what went wrong.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help With A Custom Implementation of QGraphicsSvgItem

    I did the QXmlXtreamReader route, and the error string is telling me that there was a "Premature end of document." I don't know what could be causing that. It shouldn't be the vector file itself, right? If it was, the load-directly-from-file method shouldn't be working either.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help With A Custom Implementation of QGraphicsSvgItem

    One possibility would be that the byte array data is missing a terminating element, maybe not closing the root tag.

    You could try using QDomDocument::save(), with a QTextStream that operates on a QBuffer.
    The same buffer could then also be used for the QXmlStreamReader (but it can also return its content as a QByteArray).

    Cheers,
    _

  7. #7
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help With A Custom Implementation of QGraphicsSvgItem

    Still nothing. Here's the code I'm using to try these things, on the chance that I'm doing it wrong:

    Qt:
    Qt Code:
    1.  
    2. QFile *file = new QFile(":/assets/outline.svg");
    3. file->open(QIODevice::ReadOnly);
    4. QTextStream *stream = new QTextStream(file);
    5. QString str = stream->readAll();
    6. doc->setContent(str);
    7. file->close();
    8.  
    9. QByteArray barr = doc->toByteArray();
    10.  
    11. QBuffer *buf = new QBuffer();
    12. buf->open(QIODevice::ReadWrite);
    13. QTextStream *bufStream = new QTextStream(buf);
    14. doc->save(*bufStream, 0);
    15.  
    16. QXmlStreamReader *xmlReader = new QXmlStreamReader(buf);
    17. QSvgRenderer *renderer = new QSvgRenderer();
    18.  
    19. std::cout << renderer->load(xmlReader) << std::endl;
    20. item->setSharedRenderer(renderer);
    21. scene->addItem(item);
    22. buf->close();
    To copy to clipboard, switch view to plain text mode 

    Here's the SVG file at various stages of the program:

    String input from file (characters '\r' and '\n' formatted out for readability):
    Qt Code:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
    3. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    4. <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="480px" height="360px" viewBox="0 0 480 360" enable-background="new 0 0 480 360" xml:space="preserve">
    5. <path fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" d="M317.665,110.518c-13.627,0-26.354,3.846-37.165,10.5V29.5h-200v158h51.483l-18.221,86.857l74.429,66.658l94.942-31.128l12.747-60.766c6.866,2.212,14.184,3.419,21.785,3.419c39.219,0,71.012-31.793,71.012-71.012S356.884,110.518,317.665,110.518z"/>
    6. </svg>
    To copy to clipboard, switch view to plain text mode 

    Contents of QDomDocument via doc->toByteArray() (character '\n' formatted out for readability):
    Qt Code:
    1. <?xml version='1.0' encoding='utf-8'?>
    2. <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
    3. <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
    4. <svg xmlns:xlink="http://www.w3.org/1999/xlink" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" x="0px" y="0px" height="360px" version="1.1" enable-background="new 0 0 480 360" width="480px" viewBox="0 0 480 360">
    5. <path stroke-miterlimit="10" stroke="#000000" d="M317.665,110.518c-13.627,0-26.354,3.846-37.165,10.5V29.5h-200v158h51.483l-18.221,86.857l74.429,66.658l94.942-31.128l12.747-60.766c6.866,2.212,14.184,3.419,21.785,3.419c39.219,0,71.012-31.793,71.012-71.012S356.884,110.518,317.665,110.518z" fill="#FFFFFF"/>
    6. </svg>
    To copy to clipboard, switch view to plain text mode 

    Contents of the QBuffer (character '\n' formatted out for readability):
    Qt Code:
    1. <?xml version='1.0' encoding='utf-8'?>
    2. <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
    3. <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
    4. <svg xmlns:xlink="http://www.w3.org/1999/xlink" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" x="0px" y="0px" height="360px" version="1.1" enable-background="new 0 0 480 360" width="480px" viewBox="0 0 480 360">
    5. <path stroke-miterlimit="10" stroke="#000000" d="M317.665,110.518c-13.627,0-26.354,3.846-37.165,10.5V29.5h-200v158h51.483l-18.221,86.857l74.429,66.658l94.942-31.128l12.747-60.766c6.866,2.212,14.184,3.419,21.785,3.419c39.219,0,71.012-31.793,71.012-71.012S356.884,110.518,317.665,110.518z" fill="#FFFFFF"/>
    6. </svg>
    To copy to clipboard, switch view to plain text mode 

    Every SVG file looks like it has correct syntax, albeit with a bit of reorganizing with the attribute orders. Maybe there's something in it that the QSvgRenderer doesn't like?

  8. #8
    Join Date
    Feb 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help With A Custom Implementation of QGraphicsSvgItem

    Alright, I found a workaround that works, but it's weird. Apparently the issue was with the QByteArray, as taking the output of doc->toString() and putting it into the QXmlStreamReader constructor made the whole thing work. I don't know if this is an issue of the output from QDomDocument->toByteArray or with the QSvgRenderer trying to load it or with the QByteArray itself, but as the above output shows, the data itself remains valid at every state prior to the renderer trying to load it. I hesitate to call this a fault of Qt (especially since that seems so taboo and I don't really have the experience or evidence to make a call like that), but this is a really confusing case and I can't imagine what else it could be.

Similar Threads

  1. Custom SelectionModel implementation needed?
    By graffy in forum Qt Programming
    Replies: 0
    Last Post: 23rd February 2014, 04:47
  2. QImageReader with custom QIODevice implementation
    By psih128 in forum Qt Programming
    Replies: 0
    Last Post: 10th August 2011, 03:39
  3. QGraphicsSvgItem from QDomElement
    By casperbear in forum Newbie
    Replies: 0
    Last Post: 9th September 2010, 04:25
  4. Problem when using QGraphicsSvgItem
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 20th February 2010, 17:49
  5. QGraphicsSvgItem sizing
    By jefferai in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2007, 21:48

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.