Simple way to display SVG image in a PyQt window

前端 未结 1 1807
清歌不尽
清歌不尽 2021-02-04 15:26

I\'m looking for a simple and reliable way for inserting an SVG image in a PyQt window. More precisely, I\'m wondering if it\'s possible to have an SVG image applied to a QLabel

1条回答
  •  孤城傲影
    2021-02-04 16:07

    QLabel only can load a pixmap. If You need a vector-graphic use QtSvg.QSvgWidget() and load the svg-file without converting:

    import sys
    from PyQt4 import QtGui, QtSvg
    
    app = QtGui.QApplication(sys.argv) 
    svgWidget = QtSvg.QSvgWidget('Zeichen_123.svg')
    svgWidget.setGeometry(50,50,759,668)
    svgWidget.show()
    
    sys.exit(app.exec_())
    

    or render to any subclass of QPaintDevice by QtSvg.QSvgRenderer directly, e.g. to QLabel:

    app = QtGui.QApplication(sys.argv) 
    
    widget = QtGui.QLabel()
    widget.setGeometry(50,200,500,500)
    renderer =  QtSvg.QSvgRenderer('Zeichen_123.svg')
    widget.resize(renderer.defaultSize())
    painter = QtGui.QPainter(widget)
    painter.restore()
    renderer.render(painter)
    widget.show()
    
    sys.exit(app.exec_())
    

    get Zeichen_123.svg here

    0 讨论(0)
提交回复
热议问题