Custom QIcon using QIconEngine - transparency problem
-
I'm trying to implement something like "composed icon" in Qt.
Target: I need to dynamicaly set color just for the portion of icon.
My idea: Compose this icon by two other. One icon that will be coloured as desired (perhaps by ColorizeEffect) and blend it under second icon that acts as overlay layer.
Issue: I tried QIconEngine and implementing its paint method. ColorizeEffect seems not to be working (even when I try the hack with temporary QLabel for that - when strength is set > 0.0, the QIcon formed by that is empty). But that's not the main problem. The thing is, no matter what I do, I get some default coloured background for this "composed" icon.
!http://i.stack.imgur.com/48Ozi.png(Two QToolButtons with QIcons. Left: simple QIcon, Right: my "composed" icon)!
Here is fragment of my code:
@class QComposedIconEngine: public QIconEngine { public: QComposedIconEngine(); ~QComposedIconEngine(); virtual void paint ( QPainter * painter, const QRect & rect, QIcon::Mode mode, QIcon::State state ); virtual QIconEngine * clone(void) const; public: QIcon m_qIconA; QIcon m_qIconB; QColor m_qColor; };@
And here is my implementation of paint(...):
@ void CLxQComposedIconEngine::paint ( QPainter * painter, const QRect & rect, QIcon::Mode mode, QIcon::State state )
{
QBrush brush = painter->background();
QColor color = brush.color();
brush.setColor( Qt::transparent );
painter->setBackground( brush );
painter->eraseRect( rect );
painter->setCompositionMode( QPainter::CompositionMode_SourceOver );
m_qIconA.paint( painter, rect, Qt::AlignCenter, mode, state );
};@And here is how I create that "Composed" icon:
@QComposedIconEngine * qIconEngine = new QComposedIconEngine(); QIcon i1; QIcon i2; i1.addPixmap(...); i2.addPixmap(...); qIconEngine->m_qIconA = i1; qIconEngine->m_qIconB = i2; QIcon i3( qIconEngine );@
I expect i1 and i3 to look exactly the same. And besides the damn background it really is.
But I need to make it transparent.(even when I leave my paint(...) method empty, the damn background is there!)
Does anybody know how to make the background transparent?
Thanks. -
Hi,
"setBackground":http://doc.qt.io/qt-5/qpainter.html#setBackground doesn't do what you think. IIRC you should rather do a "fillRect":http://doc.qt.io/qt-5/qpainter.html#fillRect with your transparent brush.
Hope it helps
-
Thanks SGaist, but unfortunately this doesn't help. It seems that there is either some kind of "widget" in the background of the icon or the bounding rectangle of the my composed icon defines kind of hole to the parent widgets (QToolButton & QToolBar & QWidget).
-
On what are you setting that icon ?
-
So you might be seeing the button's background since your image should be transparent
-
But the text "slozene ikony" is a part of the same button as well.
That's why I made a screenshot of two QToolButtons (each with icon and text on it). Left is with standard QIcon(filename), right is with QIcon(QComposedIconEngine). One is OK, second is not...