Results 1 to 6 of 6

Thread: SVG: access coordinates of elements

  1. #1
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Symbian S60

    Default SVG: access coordinates of elements

    Hi all,

    I want to load a file with vector graphics and do some simple modifications to each curve in it.
    So I basically need to access some coordinates within each curve (i.e. control points for Bezier curves, or just even vertices of polylines). I thought of using SVG format, loading it with QtSvg. Now the question is, after I load SVG (apparently, with QGraphicsSvgItem?), how can I look into it, i.e. access separate elements and their coordinates?

    Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: SVG: access coordinates of elements

    Use QtXML to open the SVG file as XML and manipulate it using the DOM or stream functions as appropriate.

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

    bmpix (31st August 2012)

  4. #3
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Symbian S60

    Default Re: SVG: access coordinates of elements

    Thanks, that's great, but SVG contains lots of stuff besides XML, i.e. paths need additional parsing - Qt doesn't have anything for that?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SVG: access coordinates of elements

    When Qt reads an SVG file, it builds an internal representation of the SVG XML hierarchy into an "SVG document" containing "SVG nodes". When the SVG is rendered to the screen, this document hierarchy is traversed and turned into QPainter commands. As I answered in a similar post a week or two ago, you could adapt the code in the document and node classes to give you something you could interrogate and manipulate. It would be a whole lot of work (basically duplicating everything in the classes), but it could be done.

    I would start first by looking at the internals of the QSvgRenderer class and see if you can extract the code that reads and parses the XML into the internal document. It would be nice if this could be turned into a hierarchy of QGraphicsItem instances rather than a hidden internal document structure. Then you could see and manipulate the individual child items however you want. You will probably have to invent a few new graphics item types along the way.

    It is understandable why QtSvg is implemented in this way - the reader / renderer are independent of the Graphics / View system, so an SVG file can be read and rendered anywhere. The QGraphicsSvgItem is the simplest link between these two systems, where the graphics item simply provides the place for the renderer to paint the document contents. But if you want to be able to manipulate the SVG, you need a lot more visibility than the QtSVG architecture provides.
    Last edited by d_stranz; 31st August 2012 at 18:11.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    bmpix (31st August 2012)

  7. #5
    Join Date
    Jul 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Symbian S60

    Default Re: SVG: access coordinates of elements

    Thank you guys for such detailed answers. Going through the implementation of QSvgRenderer and copying/altering the functionality seems a bit tedious to me, but maybe I will end up doing that if I don't find another library. It's weird though, the task I'm describing seems to me the most natural task to do with vector graphics, and yet I can't find any suitable solution...
    If somebody knows a library that would help, I would really appreciate it.
    Otherwise - thanks again for your help.

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: SVG: access coordinates of elements

    Quote Originally Posted by bmpix View Post
    Thanks, that's great, but SVG contains lots of stuff besides XML, i.e. paths need additional parsing - Qt doesn't have anything for that?
    Yes, sure. You can manipulate the strings you extract from the XML using any of the string manipulation faciltiies in Qt. Paths etc. are deliberately easy to parse. There is even a LALR parser add-on for Qt 5 (that predates Qt 5) in you want to build some thing more elaborate. This is all good for manipulating SVG images but not really useful for real-time, on screen manipulation which I suspect is what you were really after.

Similar Threads

  1. Print QML elements
    By merajc in forum Qt Quick
    Replies: 0
    Last Post: 15th August 2012, 23:04
  2. Ready to use QML elements
    By .:saeed:. in forum Qt Quick
    Replies: 0
    Last Post: 22nd February 2011, 16:25
  3. Replies: 4
    Last Post: 18th March 2010, 10:11
  4. Replies: 4
    Last Post: 6th August 2009, 06:12
  5. Elements out of Range!!
    By Kapil in forum Newbie
    Replies: 9
    Last Post: 3rd April 2006, 10:28

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.