Issue
I'm trying to paint an solid colored ellipse in pyside and I'm getting an unexpected black line around the outside and I'm also not getting what appears to be a smooth circular shape? Some colors are not showing up as well.
What am I doing wrong here?
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QDialog):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.resize(200, 50)
self.initUI()
def initUI(self):
self.ui_list = QtGui.QComboBox()
grid = QtGui.QVBoxLayout()
grid.addWidget(self.ui_list)
self.setLayout(grid)
self.populate_list()
def populate_list(self):
colors = {
'White': QtCore.Qt.white,
'Black': QtCore.Qt.black,
'Red': QtCore.Qt.red,
'Green': QtCore.Qt.green,
'Blue': QtCore.Qt.blue,
'Cyan': QtCore.Qt.cyan,
'Magenta': QtCore.Qt.magenta,
'Yellow': QtCore.Qt.yellow,
'Gray': QtCore.Qt.gray,
'Orange': QtGui.QColor(255,128,0)
}
px = QtGui.QPixmap(12,12)
for key, val in sorted(colors.items()):
px.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(px)
painter.setBrush(QtGui.QColor(val))
painter.drawEllipse(0,0,12,12)
self.ui_list.addItem(QtGui.QIcon(px), key)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Solution
Just looking at the approved answer, It looks to me like the outline is black and setting a larger pixmap is making the issue less visible. I would be better to set the stroke color instead. Also, making a pixmap larger in order to get a nicer looking circle is unnecessary if you turn on antialiasing explicitly.
...
for key, val in sorted(colors.items()):
# a small pixmap size
size = 32
px = QtGui.QPixmap(size,size)
px.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(px)
# turn on Antialiasing
painter.setRenderHints(QtGui.QPainter.Antialiasing, True)
# set the brush and pen to the same color
painter.setBrush(QtGui.QColor(val))
painter.setPen(QtGui.QColor(val))
painter.drawEllipse(px.rect())
painter.end()
self.ui_list.addItem(QtGui.QIcon(px), key)
Answered By - user11167350
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.