Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Load QIcon with QByteArray containing SVG vector image without creating file?

    General and Desktop
    4
    6
    2666
    Loading More Posts
    • 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.
    • A
      aatwo last edited by

      Hey guys. I have an SVG (vector) image in a Qt resource file and I am creating my applications QIcon with the SVGs resource path as a parameter. This works as expected and allows me to use one resource to supply my application icon in any size on any platform, rather than providing a ton of raster images.

      I have also written some code that can modify this icon dynamically using Qts SVG module, the end result of which is a QByteArray containing my modified SGV icon. However to load this into a QIcon I currently have to first write it to disk (using QTemporaryFile) and then create the QIcon using the temporary file path.

      I feel this disk write is an unecessary overhead and try to avoid disk writes where possible, so was wondering if Qt offers anything that will let me achieve this entirely in memory?

      Kind regards, Aaron.

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        There might be a way via
        http://doc.qt.io/qt-5/qpixmap.html#loadFromData-1
        Then into QIcon.
        Not tested :)

        1 Reply Last reply Reply Quote 1
        • A
          aatwo last edited by

          Hey. Thanks for the reply. I don't believe QPixmap::loadFromData() or QImage::loadFromData() work with vector based images since QPixmap and QImage are both pixel based image classes.

          It is possible however to render SVG images onto any paint device (including QPixmap and QImage) using the QSvgRender class, but again I want to avoid this method since loading a QIcon with a QPixmap would loose all benefits of having the icon in vector format as mentioned in my initial post.

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @aatwo last edited by

            @aatwo
            The problem with your requirement is that icons are platform and device dependent, so this wouldn't be possible. However you can render the SVG in whatever sizes you need and then convert the images to proper icons. This way you get what you want - good quality over a span of sizes.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply Reply Quote 1
            • VRonin
              VRonin last edited by VRonin

              What I would do is reimplement QIconEngine rendering the SVG in the paint method using QSvgRender and then pass this class to the constructor of your QIcon

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              kshegunov 1 Reply Last reply Reply Quote 2
              • kshegunov
                kshegunov Moderators @VRonin last edited by

                Yes, that's a good idea.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post