Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Passing SVG from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

Passing SVG from C++ to QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 1.1k 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    How do I pass an SVG which I currently have in memory on the C++ side (QByteArray or similar) on to an Image component in QML?

    For other file types such as PNG or JPEG, I would simply use QImage (or QPixmap) and a QQuickImageProvider. But that class doesn't seem to have an interface for SVGs.

    What options do I have?

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      You can paint a SVG on a QImage with QSvgRenderer and then use QQuickImageProvider.

      A 1 Reply Last reply
      0
      • GrecKoG GrecKo

        You can paint a SVG on a QImage with QSvgRenderer and then use QQuickImageProvider.

        A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        @GrecKo
        Thank you. That is of course an option.
        It just sounds awfully complicated, given that I can provide other SVGs to the Image components simply by providing a file or resource name. Especially if the Image components dynamically resizes.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Soheil
          wrote on last edited by Soheil
          #4

          @Asperamanca said in Passing SVG from C++ to QML:

          QQuickImageProvider

          You can simply pass the SVG as string to the QML side and show it using a data URI scheme (it is supported by QML Image).

          Image {
                  ...
                  source: "data:image/svg+xml;utf8," + svgString
                  ...
              }
          

          You can also do the same thing for other image formats. As most other formats are binaries, you have to convert them to Base64 string first. By the way, IMHO using QQuickImageProvider seems to be a better solution in that case.

          Image {
                  ...
                  source: "data:image/png;base64," + pngBase64String
                  ...
              }
          
          A 1 Reply Last reply
          4
          • S Soheil

            @Asperamanca said in Passing SVG from C++ to QML:

            QQuickImageProvider

            You can simply pass the SVG as string to the QML side and show it using a data URI scheme (it is supported by QML Image).

            Image {
                    ...
                    source: "data:image/svg+xml;utf8," + svgString
                    ...
                }
            

            You can also do the same thing for other image formats. As most other formats are binaries, you have to convert them to Base64 string first. By the way, IMHO using QQuickImageProvider seems to be a better solution in that case.

            Image {
                    ...
                    source: "data:image/png;base64," + pngBase64String
                    ...
                }
            
            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @Soheil
            Do you happen to know if this blob-encoding is an officially supported way to display images?
            For large images, the extra work of encoding the byte array into a QUrl seems excessive. For small SVGs, this might be a good solution for me.

            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