Important: Please read the Qt Code of Conduct -

Making QPushButton Icons Transparent.

  • Qt 5.15.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6)) on "xcb"
    OS: KDE neon User Edition 5.19 [linux version 5.4.0-48-generic]

    I've developed a little application that works, other than the fact that the transparent icons I've added are showing with the checked background rather than showing the button background.  The icons show bulbs and one is on and one is off and the application swaps them when I press the button to reflect the on/off state of the light it controls. You would change the selected light using a combobox.

    If I look at the icons in a file viewer they show as transparent images often do, with the checked background. I converted them from a non transparent background, so I can see the difference.

    This is what I did to make them Transparent:

    convert light-bulb-off.png -fuzz 1% -transparent white output1.png
    convert light-bulb-on.png -fuzz 1% -transparent white output2.png

    I'm using the same icons in two places, as an image on a button with no text and as icons in a combo-box were they would indicate the the individual state of each of the lights in the list. In both cases I want the transparent parts of the image and only those to be transparent, not the button or Combo-box. I want the user to see the widget background beneath the image.

    m_light_switch      = new QPushButton;
            m_light_switch->setIconSize(QSize(840, 859));
            connect(m_light_switch, SIGNAL (released()),this, SLOT (switchpressed()));

    If I load an icon with transparency as an application icon or within a menu, I have never had any problem getting it to display as I would expect, so I'm puzzling as to what I am doing wrong. any suggestions would be helpful.

  • Lifetime Qt Champion

    This is not supported - the background is always drawn.

  • Of course there is nothing to prevent you from designing your own pushbutton witdget to meet the requirements. You just cannot base it upon QPushbutton. You may be able to base it upon QAbstractbutton though.

Log in to reply