I have created QLabel *msgLbl
. How do I make the msgLbl
background semi-transparent?
The code for a semi-transparent blue background is: background: rgba(30, 52, 142, 0.5); Another technique for increasing the visibility of semi-transparent buttons is to give them a solid-colored border.
To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.
use opacity css property. Do you want a semi transparent background color or image? Add this to your CSS background-color:rgba(255,0,0,0.5) . Increase or decrease the 0.5 to lower or raise the opacity.
You can set transparency of QLabel or QPushbutton by setting the stylesheet : ui->label->setStyleSheet("background-color: rgba(255, 255, 255, 0);"); ui->button->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With