4

I am trying to make an svg that will be read in a QGraphicsSvgItem. I read some documentation, and it seems this is what I want:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<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="100px" height="100px" viewBox="0 0 100 100" enable-background="new 0 0 100 100"
     xml:space="preserve">
    <rect fill="#437624" stroke="none" fill-opacity="1" x="0" y="0" width="100" height="100" />

    <text y="50" transform="translate(50)">
       <tspan x="0" text-anchor="middle">No</tspan>
       <tspan x="0" text-anchor="middle" dy="15">Arrow</tspan>
   </text>
</svg>

This creates a rectangle - and a multi-line text centered inside:
enter image description here This is what it looks like in the browser.

In Qt though, when loaded in QGraphicsSvgItem, it looks like this:
enter image description here

I imagine that something is not supported by the Qt SVG renderer...

Even worse, setting the font size makes my Qt text completely disappear:

    <text y="40" font-size="24" transform="translate(50)">
       <tspan x="0" text-anchor="middle">No</tspan>
       <tspan x="0" text-anchor="middle" dy="30">Arrow</tspan>
   </text>

How can I make Qt get a multi-line centered text, as the first image, from the svg (what should I put in the SVG) ?

(Qt 4.7 to 5.5...)

Edit:

This worked (but still can't figure out how to do multi-line other than determining individual items)

<text x="0" y="40" font-size="24" transform="translate(50)" text-anchor="middle">No</text>
<text x="0" y="70" font-size="24" transform="translate(50)" text-anchor="middle">Arrow</text>

I find it puzzling that even copying svgs from tutorials, any svgs that contain tspan render correctly in browser but don't show in QGraphicsSvgItem - or perhaps they do but in a complete different location.

Thalia
  • 13,637
  • 22
  • 96
  • 190

1 Answers1

3

I believe the answer to your question is in the documentation for both QGraphicsSvg and the standard SVG Tiny 1.2.

Your library only support SVG Tiny and not the full SVG specification and while SVG Tiny does support "tspan", it also states this:

"positional attributes such as 'x', 'y', and 'rotate' are not available on 'tspan' in SVG Tiny 1.2."

See SVG Tiny 1.2: https://www.w3.org/TR/SVGTiny12/text.html#TSpanElement

See also Does QT support svg?

Community
  • 1
  • 1
Kevin Brown
  • 8,805
  • 2
  • 20
  • 38