2015-11-04 96 views
1

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

所以,如果有人可以帮助,我如何将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_()) 
+0

是否有可能使用'造型'覆盖? – JokerMartini

+0

恐怕这是不可能的。我想在几个月前解决这个问题,但没有找到解决方案。 – titusjan

回答

1

这里是一个可行的解决方案,但它需要修改前栅格化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_())