2

In Qt you can use rich text in QLabels:

QLabel* label = new QLabel("<img src=C:/img.png> In front of this text, theres an image");
label->show();

As expected, drawing the QLabel will show the image in front of the text.

Now I would like to gray out the image + text. Normally in Qt this is rather easy, because all you have to do is disable the widget:

label->setEnabled(false);

The problem now is that only the text gets grayed out by disabling, the image stays the same.

How to gray out an image included in a rich text?


EDIT to answer to comments below:

Exchanging the image with another gray version of it does work, though I had hoped for a generic solution. Taken from link:

<style>
#color
{
   background-color: red;
}
#grayscale1
{
   filter: grayscale(100%);
}
#grayscale2
{
   opacity: 0.4;
   filter: alpha(opacity=40);
}
</style>

<div id="color">
  <div id="grayscale1">
    <img src="C:/img.png" />
  </div>
</div>

The red background color works (just as a test). The grayscale1/grayscale2 dont. :(

Community
  • 1
  • 1
willard
  • 21
  • 2
  • 3
    What if you have two versions of the icon and set the grayed one along with disabling the label? – vahancho Dec 30 '15 at 13:43
  • Yhea, I don't think you can and vahancho idée is the best workaround here. – MokaT Dec 30 '15 at 13:54
  • Changing the image does work, though I had hoped for a generic solution. Added an edit to the question. – willard Dec 30 '15 at 14:31
  • Overlay the whole thing with a rectangle and set the background-color with the rgba opacity – deW1 Dec 30 '15 at 15:28
  • Are you able to use two QLabels, one with an image loaded and one with the accompanying text? You could then use setPixmap and have a QImage that is [greyed out by your program at runtime](http://stackoverflow.com/questions/27949569/convert-a-qimage-to-grayscale) for whatever images you need. The other QLabel could be greyed out using the normal setEnabled(false); – KFox Jan 17 '16 at 14:11
  • Eventually I ended up doing exactly that. Back when I asked the question I wanted to avoid that, because I wanted the layout of the picture/text mixture to be managed by Qt automagically. Think about a long text with occasional icons inbetween. Getting a correct layout with line breaks, etc affords some extra work, but it's what I'm doing now. – willard Jan 18 '16 at 15:37

0 Answers0