Simple color fill QIcons in Qt

jliu83 picture jliu83 · Nov 12, 2012 · Viewed 15.8k times · Source

I need to create a menu that changes the background of a QWidget. I'd like to place a QIcon that represents the selected color into the QActions which populates the QMenu. I'd like to not have to pop out Photoshop and paint the icons manually. Is it possible to program a simple icon that is filled with a certain color? This way I can have an arbitrary number of QActions if needed, and I won't have to make whole bunch of icons in Photoshop. \

Answer

cgmb picture cgmb · Nov 12, 2012

You can construct a QIcon from a QPixmap. QPixmap can be constructed with a given size, then filled with a colour using 'fill'.

For example, to create a red 100x100 icon:

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