Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom QIcon using QIconEngine - transparency problem

Custom QIcon using QIconEngine - transparency problem

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nohyge
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nohyge
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          On what are you setting that icon ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nohyge
            wrote on last edited by
            #5

            on QToolButton.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              So you might be seeing the button's background since your image should be transparent

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nohyge
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved