我正在尝试在pyside中绘制一个纯色的椭圆,我在外面得到一条意想不到的黑线,我也没有看到一个看似光滑的圆形形状?有些颜色也没有显示出来。
我在这里做错了什么?
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()
答案 0 :(得分:1)
你必须使用更大的QPixmap
,除此之外你必须致电painter.end()
表示你必须完成绘画:
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(640,640)
for key, val in sorted(colors.items()):
px.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(px)
painter.setBrush(QtGui.QColor(val))
painter.drawEllipse(px.rect())
painter.end()
self.ui_list.addItem(QtGui.QIcon(px), key)
答案 1 :(得分:1)
仅查看批准的答案,对我来说,轮廓像是黑色的,而设置较大的像素图会使问题更不明显。我最好设置笔触颜色。另外,如果显式打开抗锯齿功能,则不必扩大像素图以得到更好的外观。
...
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)