我尝试在运行时更改按钮图标的颜色。如果我有svg作为xml它很容易做到。是否可以从资源文件加载xml文本表示?
解决:是的,这是可能的:
#!/usr/bin/env python
import sip
try:
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
except ValueError, e:
log.error(e)
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtSvg import *
import sys
import resources_rc
if __name__ == '__main__':
app = QApplication(sys.argv)
f = QFile(":/icons/icons/svg/draw-freehand.svg")
if f.open(QFile.ReadOnly | QFile.Text):
textStream = QTextStream(f)
svgData = textStream.readAll()
f.close()
svg = QSvgRenderer(QByteArray(svgData))
qim = QImage(32, 32, QImage.Format_ARGB32)
qim.fill(0)
painter = QPainter()
painter.begin(qim)
svg.render(painter)
painter.end()
qim.save('test2.png')
答案 0 :(得分:1)
您可以将QRC中的SVG源代码读入QString并从那里进行字符串操作:
Reading from and writing to file in The Qt Resource System (qt 5.0.2)