Reputation: 845
How can I draw a SVG using qml in simple words?
According to QtDocs it is possible to draw a SVG using this code:
Path {
startX: 50; startY: 50
PathSvg { path: "L 150 50 L 100 150 z" }
}
But actually it doesn't. So, how can I draw SVG?
Upvotes: 4
Views: 4052
Reputation: 25946
Let's look at different ways for drawing this shape.svg
in QML.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 150">
<path fill="orange" stroke="transparent" d="M 50 50 L 150 50 L 100 150 z" />
Image
+ file uri or http uri Image {
source: "shape.svg"
}
Button
+ file uri Button {
background: Item { }
icon.source: "shape.svg"
icon.width: 200
icon.height: 150
icon.color: "orange"
}
Shape
+ ShapePath
+ PathSvg
: Shape {
width: 200
height: 150
ShapePath {
fillColor: "orange"
strokeWidth: 0
strokeColor: "transparent"
startX: 50; startY: 50
PathSvg { path: "L 150 50 L 100 150 z" }
}
}
Image
+ data uri Image {
source: `data:image/svg+xml;utf8,
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 150">
<path fill="orange" stroke="transparent" d="M 50 50 L 150 50 L 100 150 z" />
</svg>`
}
Upvotes: 2
Reputation: 719
If you want to show a path, you must use it within a Shape
:
Shape {
ShapePath {
fillColor: "red"
strokeWidth: 2
strokeColor: "blue"
startX: 50; startY: 50
PathSvg { path: "L 150 50 L 100 150 z" }
}
}
Upvotes: 9
Reputation: 49289
Nothing in the documentation says anything about drawing. It says "defines a path" by using SVG path syntax.
Drawing it is a different step:
Canvas {
width: 400; height: 200
contextType: "2d"
onPaint: {
context.strokeStyle = Qt.rgba(.4,.6,.8)
context.path = yourPath
context.stroke()
}
}
Note that as long as Qt is compiled with SVG support you can directly use .svg files as image sources.
Upvotes: 4