简体   繁体   中英

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. From that I wish to use the painted rectangle as a QIcon for use in a QComboBox. Any ideas? Thanks in advance.

Looks like you just need QPixmap.fill for this:

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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