你必须把图片读成
QPixmap
,使用
QPainter
修改
像素图
通过添加矩形并最终使用
像素图
创建
QIcon
import sys
from PySide import QtCore, QtGui
class Example(QtGui.QWidget):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.resize(600,400)
lay = QtGui.QHBoxLayout(self)
pixmap = QtGui.QPixmap('thumb.jpg')
painter = QtGui.QPainter(pixmap)
painter.fillRect(QtCore.QRect(20, 20, 40, 40), QtGui.QColor("red"))
painter.end()
for icon in (QtGui.QIcon('thumb.jpg'), QtGui.QIcon(pixmap)):
btn = QtGui.QPushButton()
btn.setFixedSize(128,128)
btn.setIconSize(icon.availableSizes()[0])
btn.setIcon(icon)
lay.addWidget(btn)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()