QSvgWidget animated SVG not working for animate (but ok for animateTransform)



  • Hi,

    I am trying to display an animated SVG using QSvgWidget. Works with svg's animateTranform, but does not work with simple animate. The code is as follows:

    import sys
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtSvg import QSvgWidget, QSvgRenderer
    
    # if there is only ANIMATE below (without ANIMATETRANSFORM), animated() returns false and no animation is shown.
    # with both, animated() returns true and only animateTransform plays.
    
    svg_str = """
    <svg width="120"
         height="120"
         viewBox="0 0 120 120"
         version="1.1"
         xmlns="http://www.w3.org/2000/svg">
      
      <rect x="10" y="10" width="100" height="100">
        <animateTransform
            attributeName="transform"
            begin="0s"
            dur="20s"
            type="rotate"
            from="0 60 60"
            to="360 60 60"
            repeatCount="indefinite"
            />
        <animate 
            attributeName="x"
            from="-100"
            to="120"
            dur="10s"
            repeatCount="indefinite"
            begin="0s" />
      </rect>
    </svg>
    """
    
    svg_bytes = bytearray(svg_str, encoding='utf-8')
    
    app = QApplication(sys.argv)
    
    svgWidget = QSvgWidget()
    svgWidget.renderer().load(svg_bytes)
    svgWidget.setGeometry(100,100,300,300)
    svgWidget.show()
    
    print(svgWidget.renderer().isValid())
    print(svgWidget.renderer().animated())
    
    sys.exit(app.exec_())
    

    Also tried to put the svg code in a separate svg file, as well as placing the svg widget in a QMainWindow with a layout, but it does not change anything.

    Am I missing something? Is only animateTransform supported by svg widget?



  • @supafly251 cannot say exactly but a quick search did find this from back in 2014 so its a sort of issue (functionality) that exists but has not been fully explained (that I could fine):
    https://stackoverflow.com/questions/23095732/why-do-animated-elements-in-qtsvg-need-to-be-of-type-animatetransform-in-order-t

    Also it would help greatly if you denoted what version of python and what version-&-flavor of qt that you are using

    Note I said issue (functionality) because it might not be an issue but simply how its supposed to work -- but without any specific documentation on it it is kind of hard to say. This to say looking up the QSvgRenderer there is no reference to animateTransform nor could I find any documentation for animateTransform associated with pyqt5

    Addendum: It appears (to me at least) that animateTransform is not part of pyqt at all but part of the SVG object underlying it -- you will have to dig into python's documentation on the SVG object to better understand this functionality.



  • Hello, thank you for your answer. I am using latest version of Qt (5.12 I think) with PyQt 5 and Python 3.7.3. I found that answer on SO after posting my own, and seems nobody really knows. Anyway it works, but also not with the same performance as in a browser (svg widget animation runs slower than in codepen, same machine and same code). Guess this is not the best way to programatically animate in Qt/Python. Thanks!



  • Okay so does this mean your initial question here has been resolved (at least as far as it can be) if yes then maybe you should mark this thread resolved (yes/no?) and your welcome



  • No! animate_transform implementes only a small subset of animate! At the moment this part of the SVG standard seems not implemented.

    It seems the animate keyword is completely ignored as print(svgWidget.renderer().animated()) returns false when only the animate keyword is present.



  • Okay well the SVG issue to me appears to be a python related issue and not a pyqt issue since it is the python that serves up the SVG API unless I misunderstood something when I glanced over that (aka I could be wrong it wont be the first time and most likely not the last if I am)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.