Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Displaying SVG on a QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

Displaying SVG on a QGraphicsView

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DoubleFelix
    wrote on last edited by DoubleFelix
    #1

    I've been trying to load an SVG file into a QGraphicsView object. From the error messages I get, I assume I need to use a QGraphicsScene and add my SVG to that, and then give the scene to the view. I did some searching and found a QSvgRenderer class which I can successfully load my SVG file into, but I'm not sure how to provide this to the QGraphicsScene.

    I tried this:

            svg_renderer = QSvgRenderer()
            svg_renderer.load("assets\\key_signatures\\c_sharp.svg")
            scene = QGraphicsScene()
            scene.addItem(svg_renderer)
            self.ui.c_major_display.setScene(scene)
    

    But I get errors about QGraphicsScene needing a QGraphicsItem.
    An old StackOverflow post I found says to use a QGraphicsWebView like this:

        item = QGraphicsWebView()
        item.load('file.svg')
        view = QGraphicsView()
        scene = QGraphicsScene()
        scene.addItem(item)
        view.setScene(scene)
        view.show()
        sys.exit(app.exec_())
    

    But to my knowledge, this is deprecated (it not only crashes Qt Designer, but also doesn't show up in the import list).

    I also found docs on a QGraphicsSvgItem which explicitly states its purpose is to load SVGs into a QGraphicsView, but I can't seem to import it in PySide6.
    Any ideas?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DoubleFelix
      wrote on last edited by
      #2

      Update: I figured it out. I found in the changelog for Qt 6 that much of QtSvg has been moved into QtSvgWidgets. From here, I was able to do this:

      from PySide6.QtSvgWidgets import QGraphicsSvgItem
      from PySide6.QtWidgets import QGraphicsScene
      
      svg_item = QGraphicsSvgItem("assets\\key_signatures\\c.svg")
      scene = QGraphicsScene()
      scene.addItem(svg_item)
      my_graphics_view.setScene(scene)
      
      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        One note: use forward slashes for your paths. It's less error prone and Qt handles the conversion if needed.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          One note: use forward slashes for your paths. It's less error prone and Qt handles the conversion if needed.

          D Offline
          D Offline
          DoubleFelix
          wrote on last edited by
          #4

          @SGaist I just kind of threw it in there as a placeholder. Does Qt handle it completely, or should I still use os.path.join to be safe?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Qt handles the conversion internally so you'll be fine. Currently you have a Windows specific path so not really portable.

            If you want to stay in the Python land, you should use pathlib and convert the path to a string before passing it to Qt.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved