简体   繁体   English

通过PyQT中的QPainter构造QIcon

[英]Constructing a QIcon through a QPainter in PyQT

I know this is possible, but I cannot for the life of me get the proper code to work. 我知道这是可能的,但是我无法终生获得正确的代码来工作。 What I want is very simple: a monochromatic rectangle, size, say 20x20 constructed (presumably) through a QPainter. 我想要的非常简单:一个单色矩形,大小为20x20(大概)是通过QPainter构造的。 From that I wish to use the painted rectangle as a QIcon for use in a QComboBox. 因此,我希望将绘制的矩形用作在QComboBox中使用的QIcon。 Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

Looks like you just need QPixmap.fill for this: 看起来您只需要QPixmap.fill

from PyQt4 import QtGui

class Window(QtGui.QComboBox):
    def __init__(self):
        QtGui.QComboBox.__init__(self)
        self.resize(200, 25)
        pixmap = QtGui.QPixmap(20, 20)
        for color in 'red orange yellow green blue grey violet'.split():
            pixmap.fill(QtGui.QColor(color))
            self.addItem(QtGui.QIcon(pixmap), color.title())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM