Simple QIcons color fills in Qt

I need to create a menu that will change the QWidget background. I would like to place a QIcon that represents the selected color in QActions that fills QMenu. I would not want to jump out of Photoshop and draw the icons manually. Is it possible to program a simple icon filled with a specific color? That way, I can have any number of QActions if necessary, and I don’t have to create a whole bunch of icons in Photoshop. \

+6
source share
2 answers

You can build QIcon from QPixmap. QPixmap can be designed with a given size, then filled with color using "fill".

For example, to create a red 100x100 icon:

QPixmap pixmap(100,100); pixmap.fill(QColor("red")); QIcon redIcon(pixmap); 
+12
source

I just figured out how to change the color from the icon to any other color. Therefore, the icon image should consist of one solid color (here: "black"), which can be converted using pixmap and its masking ability into another color (for example, "red"):

 pixmap = QPixmap(filename) mask = pixmap.createMaskFromColor(QColor('black'), Qt.MaskOutColor) pixmap.fill((QColor('red'))) pixmap.setMask(mask) btNew = QToolButton() btNew.setIcon(QIcon(pixmap)) 
+5
source

All Articles