Pyside在QIcon中改变SVG的颜色或不透明度

时间:2015-11-04 02:48:06

标签: python qt svg pyside

我一直在网上阅读,还没有找到解决方案。我想要做的是改变每次使用的图标的颜色,或改变它的不透明度。

因此,如果有人可以提供帮助,我如何将SVG图标'Vimeo'的颜色更改为红色或蓝色而不是创建多个图像?

链接到svg:https://www.dropbox.com/s/vshvosnuu5998wy/vimeo.svg?dl=0

enter image description here

# Modules
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore, QtSvg

# widget
# ------------------------------------------------------------------------------
class Example(QtGui.QWidget):

    def __init__(self,):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        # formatting
        self.setGeometry(300, 300, 600, 300)
        self.setWindowTitle("Example")

        # widgets
        self.itemList = QtGui.QTreeWidget()
        self.itemList.setItemsExpandable(True)
        self.itemList.setAnimated(True)
        self.itemList.setItemsExpandable(True)
        self.itemList.setColumnCount(2)
        self.itemList.setHeaderLabels(['', ''])

        # load some icons
        self._ico_01 = QtGui.QIcon('vimeo.svg')

        # add items
        item0 = QtGui.QTreeWidgetItem(self.itemList, ['testing', ''])
        item0.setIcon(1, self._ico_01)  # 1 - we set image for second colomn

        item1 = QtGui.QTreeWidgetItem(self.itemList, ['testing', ''])
        item1.setIcon(1, self._ico_01)  # 1 - we set image for second colomn

        # layout
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.addWidget(self.itemList)
        self.show()

# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

这是一个有效的解决方案,但它需要在修改前光栅化svg。 这是在运行时完成的(在此处以500px烘焙)(感谢This SO answer

svg转换为QImage并在创建图标时以选择的颜色绘制。

# Modules
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore, QtSvg
from random import randint

# widget
# ------------------------------------------------------------------------------
class Example(QtGui.QWidget):

    def __init__(self,):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        # formatting
        self.setGeometry(300, 300, 600, 300)
        self.setWindowTitle("Example")

        # widgets
        self.itemList = QtGui.QTreeWidget()
        self.itemList.setItemsExpandable(True)
        self.itemList.setAnimated(True)
        self.itemList.setItemsExpandable(True)
        self.itemList.setColumnCount(2)
        self.itemList.setHeaderLabels(['', ''])

        # Load the svg
        renderer = QtSvg.QSvgRenderer('D:/DEV/TMP/vimeo.svg')        
        # Prepare a QImage with desired characteritisc
        self.orig_svg = QtGui.QImage(500, 500, QtGui.QImage.Format_ARGB32);    
        # Get QPainter that paints to the image
        painter = QtGui.QPainter(self.orig_svg);
        renderer.render(painter);

        # add items
        color0 = QtGui.QColor( 255, 35, 35 )
        item0 = QtGui.QTreeWidgetItem(self.itemList, ['testing', ''])
        item0.setIcon(1, self.icon_colored( color0 ))  # 1 - we set image for second colomn

        color1 = QtGui.QColor( 32, 255, 35 )
        item1 = QtGui.QTreeWidgetItem(self.itemList, ['testing', ''])
        item1.setIcon(1, self.icon_colored( color1 ) )  # 1 - we set image for second colomn


        pixmap = QtGui.QPixmap.fromImage( self.orig_svg )
        self.lbl = QtGui.QLabel(self)
        self.lbl.setPixmap(pixmap)

        self.button = QtGui.QPushButton("rand color")

        self.button.clicked.connect( self.changeColor )

        # layout
        self.mainLayout = QtGui.QVBoxLayout(self)
        self.mainLayout.addWidget(self.button)
        self.mainLayout.addWidget(self.itemList)
        self.mainLayout.addWidget(self.lbl)
        self.show()

    @QtCore.Slot()
    def changeColor( self ):

        r = randint(0,255)
        g = randint(0,255)
        b = randint(0,255)

        # Copy the image
        new_image = self.orig_svg.copy()

        # We are going to paint a plain color over the alpha
        paint = QtGui.QPainter()
        paint.begin( new_image )
        paint.setCompositionMode( QtGui.QPainter.CompositionMode_SourceIn )
        paint.fillRect( new_image.rect(), QtGui.QColor( r, g, b ) )

        paint.end()

        self.lbl.setPixmap( QtGui.QPixmap.fromImage(new_image) )

    def icon_colored( self, color ):

        # Copy the image
        new_image = self.orig_svg.copy()

        # We are going to paint a plain color over the alpha
        paint = QtGui.QPainter()
        paint.begin( new_image )        
        paint.setCompositionMode( QtGui.QPainter.CompositionMode_SourceIn )       
        paint.fillRect( new_image.rect(), color )        
        paint.end()

        return QtGui.QIcon( QtGui.QPixmap.fromImage( new_image ) )


app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())