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. Qt SVG : Dynamic Modification
Qt 6.11 is out! See what's new in the release blog

Qt SVG : Dynamic Modification

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
24 Posts 9 Posters 18.1k Views 4 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.
  • fcarneyF fcarney

    @mzimmers I use Shape for complex dynamic things that can change. It works really well.

    mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #10

    @fcarney in this case, I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

    fcarneyF JKSHJ 4 Replies Last reply
    0
    • mzimmersM mzimmers

      @fcarney in this case, I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

      fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #11

      @mzimmers Ah, yeah, that sucks.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • mzimmersM mzimmers

        @fcarney in this case, I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

        fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #12

        @mzimmers Maybe you can do something with PathSVG. I played with this a bit for my own stuff. Might be able to pick apart the file and color each the way you want.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        1
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #13

          @fcarney that's definitely on the right track. Looks like I need to find (or write) a .svg to Qt Path converter.

          1 Reply Last reply
          0
          • mzimmersM mzimmers

            @fcarney in this case, I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

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

            @mzimmers said in Qt SVG : Dynamic Modification:

            this necessitates file level changes. Impractical, and (I think) impossible when the files are embedded as resources.

            You could do string replacements and pass the final document to an Image as a Data URL:

            Window {
                width: 640
                height: 480
                visible: true
            
                property string svgStr: `<svg xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="50" fill="${colours.currentText}"/></svg>`
            
                ComboBox {
                    id: colours
                    model: ["red", "green", "blue"]
                }
            
                Image {
                    anchors.centerIn: parent
                    source: `data:image/svg+xml;base64,${Qt.btoa(svgStr)}`
                    sourceSize.width: 300
                    sourceSize.height: 300
                }
            }
            

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

            mzimmersM 1 Reply Last reply
            1
            • JKSHJ JKSH

              @mzimmers said in Qt SVG : Dynamic Modification:

              this necessitates file level changes. Impractical, and (I think) impossible when the files are embedded as resources.

              You could do string replacements and pass the final document to an Image as a Data URL:

              Window {
                  width: 640
                  height: 480
                  visible: true
              
                  property string svgStr: `<svg xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="50" fill="${colours.currentText}"/></svg>`
              
                  ComboBox {
                      id: colours
                      model: ["red", "green", "blue"]
                  }
              
                  Image {
                      anchors.centerIn: parent
                      source: `data:image/svg+xml;base64,${Qt.btoa(svgStr)}`
                      sourceSize.width: 300
                      sourceSize.height: 300
                  }
              }
              
              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #15

              @JKSH this is probably the way to go for most cases, and (I imagine) solves the OP's question. (I'll need to build around this, as I'm currently using the icon property of TabButton.)

              @fcarney @JKSH this has been educational; thanks.

              L JKSHJ 2 Replies Last reply
              0
              • mzimmersM mzimmers

                @JKSH this is probably the way to go for most cases, and (I imagine) solves the OP's question. (I'll need to build around this, as I'm currently using the icon property of TabButton.)

                @fcarney @JKSH this has been educational; thanks.

                L Offline
                L Offline
                lemons
                wrote on last edited by
                #16

                @mzimmers if it is only for icons, did you think about simply converting them into a ligature font (named icons). This way you can use the icons as font in a QML Text element.

                Text {
                    font.family: "myLigatureFontName" // e.g. FontAwesome Pro
                    font.pointSize: 32
                    text: "myLigatureIconName" // e.g. "home" for a house-icon
                    color: "transparent" // font color
                    style: Text.Outline  // if you need outline
                    styleColor: "red"    // outline color
                }
                
                1 Reply Last reply
                1
                • mzimmersM mzimmers

                  @JKSH this is probably the way to go for most cases, and (I imagine) solves the OP's question. (I'll need to build around this, as I'm currently using the icon property of TabButton.)

                  @fcarney @JKSH this has been educational; thanks.

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

                  @mzimmers said in Qt SVG : Dynamic Modification:

                  (I'll need to build around this, as I'm currently using the icon property of TabButton.)

                  @fcarney @JKSH this has been educational; thanks.

                  You're welcome.

                  Works for button icons too!

                  TabButton {
                      text: "Click Me"
                      icon.source: `data:image/svg+xml;base64,${Qt.btoa(svgStr)}`
                  }
                  

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

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    mr_broccoli
                    wrote on last edited by mr_broccoli
                    #18

                    @Parvathy2020 I know this is late, but still.
                    I have had a very similar issue where we needed to change the color from SVG regions. Our solution was quite unique actually:

                    Use a QGraphicsView with a scene ofcourse and then use the QGraphicsSvgItem (not the normal one). You can then get the svg item by setElementId().
                    If you use a colorizer on top, you can manipulate the QGraphicsSvgRenderer to overwrite that area with a new color. No need to alter the file directly.

                    //create objects
                    QGraphicsSvgItem* newSvgItem = new QGraphicsSvgItem();
                    QGraphicsColorizeEffect* newColorizeEffect = new QGraphicsColorizeEffect(nullptr);
                    
                    //--- setup SVG ---
                    //setup renderer
                    newSvgItem->setSharedRenderer(&svgRenderer);
                    
                    //"draw" element on correct position
                    newSvgItem->setElementId(elementId.toString());
                    newSvgItem->setPos(svgRenderer.boundsOnElement(elementId.toString()).left(), svgRenderer.boundsOnElement(elementId.toString()).top());
                    
                    //--- colorize effect ---
                    newColorizeEffect->setStrength(colorStrength);
                    newColorizeEffect->setColor(color);
                    newColorizeEffect->setEnabled(true);
                    
                    //link graphics effect
                    newSvgItem->setGraphicsEffect(newColorizeEffect);
                    
                    //setup scene	
                    scene.addItem(newSvgItem);
                    

                    This will select a very specific element from the SVG and adds it to the scene. This is how you can filter as well.
                    On top of that, if you keep track of your colorizer (using a map or whatever), you can change the color dynamically.

                    https://doc.qt.io/archives/qt-5.15/qgraphicssvgitem.html

                    1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      @fcarney in this case, I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

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

                      @mzimmers said in Qt SVG : Dynamic Modification:

                      I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

                      This did not exist at the time of our original discussion, but Qt 6.8 introduced the svgtoqml tool: https://doc.qt.io/qt-6/qtqml-tooling-svgtoqml.html Each element gets converted to a Qt Quick Shape which you can colour individually.

                      This would be more efficient than the Data URL method I described earlier, because this doesn't involve re-parsing the whole SVG file every time a colour is updated.

                      @mr_broccoli said in Qt SVG : Dynamic Modification:

                      Use a QGraphicsView with a scene ofcourse and then use the QGraphicsSvgItem (not the normal one). You can then get the svg item by setElementId().
                      If you use a colorizer on top, you can manipulate the QGraphicsSvgRenderer to overwrite that area with a new color. No need to alter the file directly.

                      Thanks for sharing your alternative solution! This is suitable for projects that use Qt Widgets (which are not GPU accelerated, unlike Qt Quick).

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

                      jsulmJ M 2 Replies Last reply
                      1
                      • JKSHJ JKSH

                        @mzimmers said in Qt SVG : Dynamic Modification:

                        I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

                        This did not exist at the time of our original discussion, but Qt 6.8 introduced the svgtoqml tool: https://doc.qt.io/qt-6/qtqml-tooling-svgtoqml.html Each element gets converted to a Qt Quick Shape which you can colour individually.

                        This would be more efficient than the Data URL method I described earlier, because this doesn't involve re-parsing the whole SVG file every time a colour is updated.

                        @mr_broccoli said in Qt SVG : Dynamic Modification:

                        Use a QGraphicsView with a scene ofcourse and then use the QGraphicsSvgItem (not the normal one). You can then get the svg item by setElementId().
                        If you use a colorizer on top, you can manipulate the QGraphicsSvgRenderer to overwrite that area with a new color. No need to alter the file directly.

                        Thanks for sharing your alternative solution! This is suitable for projects that use Qt Widgets (which are not GPU accelerated, unlike Qt Quick).

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #20

                        @JKSH said in Qt SVG : Dynamic Modification:

                        which are GPU accelerated, unlike Qt Quick

                        Isn't it other way around?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        JKSHJ 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @JKSH said in Qt SVG : Dynamic Modification:

                          which are GPU accelerated, unlike Qt Quick

                          Isn't it other way around?

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

                          @jsulm said in Qt SVG : Dynamic Modification:

                          @JKSH said in Qt SVG : Dynamic Modification:

                          which are GPU accelerated, unlike Qt Quick

                          Isn't it other way around?

                          Oops! Forgot a "not". Thanks! 😅

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

                          1 Reply Last reply
                          0
                          • JKSHJ JKSH

                            @mzimmers said in Qt SVG : Dynamic Modification:

                            I'm given .svg files from the UX team. I'd have to translate them into QML Shapes, which I suppose isn't terrible, but being the creatively lazy guy I am, I'm hoping for something more straightforward.

                            This did not exist at the time of our original discussion, but Qt 6.8 introduced the svgtoqml tool: https://doc.qt.io/qt-6/qtqml-tooling-svgtoqml.html Each element gets converted to a Qt Quick Shape which you can colour individually.

                            This would be more efficient than the Data URL method I described earlier, because this doesn't involve re-parsing the whole SVG file every time a colour is updated.

                            @mr_broccoli said in Qt SVG : Dynamic Modification:

                            Use a QGraphicsView with a scene ofcourse and then use the QGraphicsSvgItem (not the normal one). You can then get the svg item by setElementId().
                            If you use a colorizer on top, you can manipulate the QGraphicsSvgRenderer to overwrite that area with a new color. No need to alter the file directly.

                            Thanks for sharing your alternative solution! This is suitable for projects that use Qt Widgets (which are not GPU accelerated, unlike Qt Quick).

                            M Offline
                            M Offline
                            mr_broccoli
                            wrote on last edited by
                            #22

                            @JKSH

                            ... This is suitable for projects that use Qt Widgets (which are not GPU accelerated, unlike Qt Quick).

                            What do you mean it is not GPU accelerated? My understanding is that QWidgets use the QPainter device, which is not GPU accelerated. Whiles the Graphics View does. Would you mind pointing out how I could optimise then?

                            Thanks!

                            SGaistS 1 Reply Last reply
                            0
                            • M mr_broccoli

                              @JKSH

                              ... This is suitable for projects that use Qt Widgets (which are not GPU accelerated, unlike Qt Quick).

                              What do you mean it is not GPU accelerated? My understanding is that QWidgets use the QPainter device, which is not GPU accelerated. Whiles the Graphics View does. Would you mind pointing out how I could optimise then?

                              Thanks!

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #23

                              @mr_broccoli the graphics view (QGraphicsView) can make use of a QOpenGLWidget as viewport in order to get acceleration but it's not the default.

                              See here

                              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
                              2
                              • EskilE Offline
                                EskilE Offline
                                Eskil
                                wrote on last edited by
                                #24

                                The new svgtoqml tool and VectorImage component might be interesting for this. This will convert the SVG to QML code. The rendering will then be hardware-accelerated (being just Qt Quick under the hood).

                                In addition, since it generates a tree of Qt Quick items with object names based on the IDs given in the source file, it is possible to traverse it using JavaScript and change properties on specific items as the original post here requested.

                                Some more details can be found in these blogs if you are interested:

                                https://www.qt.io/blog/vector-graphics-in-qt-6.8
                                https://www.qt.io/blog/animated-vector-graphics-in-qt-6.10

                                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