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. Rendering SVG images in QIcons natively not supported

Rendering SVG images in QIcons natively not supported

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 5 Posters 2.5k Views
  • 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
    DanilZ
    wrote on last edited by DanilZ
    #1

    The problem is that the SVG images imported into resources.qrc are not properly displayed by the Ui_MainWindow class without it's modifications.

    1. I am using the QT Designer Qt version 6.3.1
    2. I am creating the resources.qrc file and filling it with .svg icons
    3. I am creating my application in QT Designer, all icons are displayed correctly at preview
      Screenshot 2022-08-07 at 18.05.01.jpg
    4. I save the .ui file
    5. I transform it into .py file with:
    $ pyside6-uic ui_file_name.ui > python_file_name.py
    
    1. I transform the resources file into .py file with:
    $ pyside6-rcc icons.qrc -o icons_rc.py
    

    And my application does not display the svg icons at runtime...

    After having researched the web, I have noticed that after transforming the .ui file into .py it calls for .svg resources like so:

    icon = QIcon()
    icon.addFile(u":/icons/icons/book-open.svg", QSize(), QIcon.Normal, QIcon.Off)
    
    

    Changing it manually to the below solves the problem:

    from PySide6 import QtSvg
    from PySide6 import QtGui
    
    svg_renderer = QtSvg.QSvgRenderer(":/icons/icons/book-open.svg")
    image = QtGui.QImage(64,64, QtGui.QImage.Format_ARGB32)
    image.fill(0x00000000)
    svg_renderer.render(QtGui.QPainter(image))
    pixmap = QtGui.QPixmap.fromImage(image)
    icon = QIcon(pixmap)
    

    But it has to be done to each one of the dozens of my icons every time I update the .ui file with QT Designer...

    I was wandering - is there a more proper way to generate .ui files from the QT designer or to transform them into .py with native support for .svg resources? There must be some better way than my reiterative manual hassle.

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

      Hi,

      Are you importing your resource file somewhere in your code ?

      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
      0
      • D DanilZ

        The problem is that the SVG images imported into resources.qrc are not properly displayed by the Ui_MainWindow class without it's modifications.

        1. I am using the QT Designer Qt version 6.3.1
        2. I am creating the resources.qrc file and filling it with .svg icons
        3. I am creating my application in QT Designer, all icons are displayed correctly at preview
          Screenshot 2022-08-07 at 18.05.01.jpg
        4. I save the .ui file
        5. I transform it into .py file with:
        $ pyside6-uic ui_file_name.ui > python_file_name.py
        
        1. I transform the resources file into .py file with:
        $ pyside6-rcc icons.qrc -o icons_rc.py
        

        And my application does not display the svg icons at runtime...

        After having researched the web, I have noticed that after transforming the .ui file into .py it calls for .svg resources like so:

        icon = QIcon()
        icon.addFile(u":/icons/icons/book-open.svg", QSize(), QIcon.Normal, QIcon.Off)
        
        

        Changing it manually to the below solves the problem:

        from PySide6 import QtSvg
        from PySide6 import QtGui
        
        svg_renderer = QtSvg.QSvgRenderer(":/icons/icons/book-open.svg")
        image = QtGui.QImage(64,64, QtGui.QImage.Format_ARGB32)
        image.fill(0x00000000)
        svg_renderer.render(QtGui.QPainter(image))
        pixmap = QtGui.QPixmap.fromImage(image)
        icon = QIcon(pixmap)
        

        But it has to be done to each one of the dozens of my icons every time I update the .ui file with QT Designer...

        I was wandering - is there a more proper way to generate .ui files from the QT designer or to transform them into .py with native support for .svg resources? There must be some better way than my reiterative manual hassle.

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @DanilZ said in Rendering SVG images in QIcons natively not supported:

        native support for .svg resources

        QIcon supports SVG images via an "icon engine" plugin.

        I would check if your plugin is correctly installed/deployed (for example, plugins/iconengines/qsvgicon.dll)

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        D 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Are you importing your resource file somewhere in your code ?

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

          @SGaist Yes, of course. Apart from the SVG icons I have one PNG icon and it is displayed correctly:

          Screenshot 2022-08-08 at 10.52.40.jpg

          1 Reply Last reply
          0
          • JKSHJ JKSH

            @DanilZ said in Rendering SVG images in QIcons natively not supported:

            native support for .svg resources

            QIcon supports SVG images via an "icon engine" plugin.

            I would check if your plugin is correctly installed/deployed (for example, plugins/iconengines/qsvgicon.dll)

            D Offline
            D Offline
            DanilZ
            wrote on last edited by
            #5

            @JKSH There seems to be some other svg plugin, this one:

            Screenshot 2022-08-08 at 10.54.02.jpg

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              I am a C++ guy and don't know exactly how this translates to Python. However, to be able to use SVG icons in a qmake-based project I need to add QT += svg. You should have something similar in your project to get it working.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Georgios G.
                wrote on last edited by
                #7

                Simply import the library :
                from PySide6 import QtSvg

                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