Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple color fill QIcons in Qt

Tags:

c++

qt

qicon

qmenu

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. \

like image 884
jliu83 Avatar asked Nov 12 '12 19:11

jliu83


2 Answers

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);
like image 132
cgmb Avatar answered Sep 23 '22 10:09

cgmb


Just figured out how to change color from icon to any other color. Therefore, the image of the icon must consist of one solid color (here: 'black') which can be converted using a pixmap and its masking ability into another color (like 'red'):

pixmap = QPixmap(filename)
mask = pixmap.createMaskFromColor(QColor('black'), Qt.MaskOutColor)
pixmap.fill((QColor('red')))
pixmap.setMask(mask)

btNew = QToolButton()
btNew.setIcon(QIcon(pixmap))
like image 45
Henhuy Avatar answered Sep 24 '22 10:09

Henhuy