Python Pyside在QIcon中更改SVG的颜色或不透明度
我一直在网上阅读,还没有找到解决办法。我想做的是改变每次使用图标的颜色,或者改变它的不透明度 因此,如果有人能够提供帮助,我如何将SVG图标“Vimeo”的颜色更改为红色或蓝色,而不是创建多个图像 链接到svg:Python Pyside在QIcon中更改SVG的颜色或不透明度,python,qt,svg,pyside,Python,Qt,Svg,Pyside,我一直在网上阅读,还没有找到解决办法。我想做的是改变每次使用图标的颜色,或者改变它的不透明度 因此,如果有人能够提供帮助,我如何将SVG图标“Vimeo”的颜色更改为红色或蓝色,而不是创建多个图像 链接到svg: 这是一个可行的解决方案,但需要在修改之前将svg光栅化。 这是在运行时完成的(在这里以500px的速度烘焙)(感谢) svg将转换为QImage,并在图标创建时以choosen颜色绘制 # Modules # -------------------------------------
这是一个可行的解决方案,但需要在修改之前将svg光栅化。 这是在运行时完成的(在这里以500px的速度烘焙)(感谢) svg将转换为QImage,并在图标创建时以choosen颜色绘制
# 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_())
可能使用“样式化”覆盖吗?恐怕不可能。几个月前我也想解决这个问题,但没有找到解决办法。
# 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_())