I'm working with Qt, trying to prototype a "clickable SVG" widget. I'm trying to get accurate coordinates for a particular element within an SVG from a QSvgWidget.
I have the following very rough code for the purposes of working out how to do this. What I'm trying to do is to programmatically get the "foo" QLabel to have the same position as the "foo" rectangle in the displayed SVG.
from PySide2.QtWidgets import QApplication, QWidget, QLabel, QMainWindow
from PySide2.QtSvg import QSvgWidget
from PySide2.QtCore import QTimer
import sys
app = QApplication(sys.argv)
window = QMainWindow()
central = QSvgWidget("test_svg.svg")
foo = QLabel("foo", parent=central)
window.setCentralWidget(central)
bound1 = central.renderer().boundsOnElement("foo")
bound2 = central.renderer().transformForElement("foo").mapRect(bound1)
tl = bound2.topLeft()
foo.move(tl.x(), tl.y())
window.show()
app.exec_()
Here's the SVG:
<svg height="400" width="450" viewbox="0 0 450 400" xmlns="http://www.w3.org/2000/svg">
<circle stroke="red" fill="none" cx="50" cy="50" r="30" />
<rect id="foo" stroke="blue" fill="none" x="100" y="300" width="20" height="5" />
</svg>
Here's what I see on-screen:
When I debug what's going on, I see that bound1.topLeft() and bound2.topLeft() are the same, and are equal to PySide2.QtCore.QPointF(99.500000, 299.500000). which is almost where the "foo" element is in SVG coordinates.
I'm not sure exactly what the correct terminology is, but let me define the SVG coordinate system as the one used in the SVG file and the widget coordinate system as the one which corresponds to the actual pixel position where something is rendered in pixels relative to the top-left corner of the widget in question.
So the QSvgWidget appears to be giving me SVG coordinates rather than "widget coordinates".
Does anyone know if there is a canonical way for me to get Qt to give me coordinates which are "accurate" for these purposes? I can probably work it out by using the widget size to work out a scaling factor etc., but it seems likely that Qt has a better way than this. After all, at some point Qt must know the actual scaled coordinates in order to do the rendering.
