Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Load QIcon with QByteArray containing SVG vector image without creating file?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 3.2k Views 2 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.
  • A Offline
    A Offline
    aatwo
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • A Offline
        A Offline
        aatwo
        wrote on last edited by
        #3

        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.

        kshegunovK 1 Reply Last reply
        0
        • A aatwo

          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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @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
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            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

            kshegunovK 1 Reply Last reply
            2
            • VRoninV 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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              Yes, that's a good idea.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              • Login

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