Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I add this answer because I think it could be useful to anybody.</p> <p>I step into the problem of setting <a href="https://en.wikipedia.org/wiki/RGBA_color_space" rel="nofollow noreferrer">RGBA</a> colors (that is, RGB color with an Alpha value for transparency) for color display labels in my painting application.</p> <p>As I came across the first answer, I was unable to set an RGBA color. I have also tried things like: </p> <p><code>myLabel.setStyleSheet("QLabel { background-color : %s"%color.name())</code> </p> <p>where <code>color</code> is an RGBA color.</p> <p>So, my dirty solution was to extend <code>QLabel</code> and override <code>paintEvent()</code> method filling its bounding rect.</p> <p>Today, I've open up the <code>qt-assistant</code> and read the <a href="https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties" rel="nofollow noreferrer">style reference properties list</a>. Affortunately, it has an example that states the following:</p> <p><code>QLineEdit { background-color: rgb(255, 0, 0) }</code></p> <p>Thats open up my mind in doing something like the code below, as an example:</p> <pre class="lang-py prettyprint-override"><code>myLabel= QLabel() myLabel.setAutoFillBackground(True) # This is important!! color = QtGui.QColor(233, 10, 150) alpha = 140 values = "{r}, {g}, {b}, {a}".format(r = color.red(), g = color.green(), b = color.blue(), a = alpha ) myLabel.setStyleSheet("QLabel { background-color: rgba("+values+"); }") </code></pre> <p>Note that <code>setAutoFillBackground()</code> set in <code>False</code> will not make it work.</p> <p>Regards,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload