Issue
How can I append a painted rectangle to a QIcon. The final returned result has to be a qicon because I'm using this on a control which expects a qicon.
Before:
After:
import os, 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)
self.btn = QtGui.QPushButton()
self.btn.setFixedSize(128,128)
icon = QtGui.QIcon('thumb.jpg')
self.btn.setIconSize(icon.availableSizes()[0])
self.btn.setIcon(icon)
lay = QtGui.QVBoxLayout()
lay.addWidget(self.btn)
self.setLayout(lay)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Solution
You have to read the image as QPixmap
, use QPainter
to modify the QPixmap
by adding the rectangle and finally use the QPixmap
to create the 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()
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.