Important: Please read the Qt Code of Conduct -

QSvgWidget and URIs: Can I emit a signal by clicking on a link in an SVG image?

  • What I would like to do is this:
    I can display an SVG image of a graph, which is generated by the Graphviz library, in a QSvgWidget (no problem there). Nodes and edges of the graph can have tooltips as well as URL properties. When the SVG file is displayed in a browser, the link specified by the URL is followed if one clicks on it. Instead, since it is being displayed in a widget and not a browser, I want to emit a QObject signal which would be connected to the parent of the QSvgWidget and cause it to display a popup window with additional data related to the node or edge.

    What is the best way to do this?

  • Lifetime Qt Champion

    The only way know is to read the XML /with XML class) and find the Id that are links.
    Then use
    QRectF QSvgRenderer::boundsOnElement(const QString &id) const
    to get the rects for the choosen Ids.
    Then use this rect list for mousePress event on a QSvgWidget subclass

    I did not find any access methods for SVG properties in the SVG classes.

    That said, Graphviz also supports HREF in the dot format so you can make labels

  • @mrjj Thanks very much, this was very helpful!

    In the meantime, I discovered that Graphviz can also output an HTML image map of a graph. Together with the information returned by QSvgRenderer::boundsOnElement function, I think I can do it.

    As to the link ID: Is this the tag name, or is there an attribute "id" for each SVG element? (I don't know much about SVG except that it is a special kind of XML format.)

  • Lifetime Qt Champion

    Yes Graphviz is very nice. Many ways to do things.
    its the ID attribute all elements have.
    <path id="lineBC" d="M 250 50 l 150 300" stroke="red"

    lineBC in this case.

    Since the id allows us to get a rect to hit test with , it was possible to know it was clicked.
    However, i did not find other way to read say text of a text element to get any URL
    so i think you need to find out this by reading the XML/or the HTML.

    Well SVGs format is just plain XML. So it should not too complicated to dig out
    some URLS.

  • @mrjj Thank you!

  • Lifetime Qt Champion

    Np :)
    Just as a note. I used inkscape to fiddle with the SVGs.
    Its sorts of reverses the element Y pos if saved.
    So just in case you did use InkScape as that fooled me for some time.

Log in to reply