I have created QLabel *msgLbl
. How do I make the msgLbl
background semi-transparent?
The easiest way is probably to call setStylesheet()
. If you're using Qt Designer you can also set the stylesheet from within the designer window (look in the properties bar on the right.
You want to set the background-color
attribute, so something like
msgLbl->setStyleSheet("background-color: rgba(255, 255, 255, 10);");
would be the simplest way to do what you describe.
Having said that, you might also want to think about stylesheet inheritance. For example, you might want to set the background-color
for a number of QLabel
s, all children of a parent widget. You can do that using css-style selectors in a stylesheet set on the parent widget (read this for more information).
-
Thanks for the reply! But i want to make the background semi-transparent. – Shyam Oct 28 '11 at 12:40
-
3The code above should do that: notice you're setting the
background-colour
using rgba. The a stands for alpha, meaning transparency. An alpha setting of 255 is completely opaque, and an alpha of 0 is completely transparent, sorgba(255, 255, 255, 10)
is a very transparent white. – sam-w Oct 28 '11 at 12:54 -
Thanks! Now i changed to 'background-color:rgba(182,159,64,230)' and working fine!! – Shyam Oct 28 '11 at 13:10
-
@Shyam If that answer was helpful to you, you can vote it up, and/or accept it. See that part of the FAQ for details. – alexisdm Oct 28 '11 at 16:29
-
@sjwarner Even when I use
(255, 255, 255, 0)
I can still see the label box contrasted against my background image... How to solve this? – the_prole Dec 5 '14 at 17:03