代码之家  ›  专栏  ›  技术社区  ›  JokerMartini

用Pyside绘制填充椭圆

  •  1
  • JokerMartini  · 技术社区  · 7 年前

    我试图在pyside中绘制一个纯色的椭圆,但我在外面得到了一条意想不到的黑线,而且我也没有得到一个看起来平滑的圆形?一些颜色也没有显示出来。

    我做错了什么?

    enter image description 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()
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   user11167350    6 年前

    只要看看批准的答案,在我看来,轮廓是黑色的,设置一个更大的pixmap会使问题变得不那么明显。我最好设置笔划颜色。此外,如果显式启用抗锯齿,则无需将pixmap放大以获得更漂亮的圆。

    ...

    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) 
    
        2
  •  1
  •   eyllanesc Yonghwan Shin    7 年前

    你必须使用更大的 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) 
    

    enter image description here