PDA

View Full Version : QTextEdit with semi-transparent background



ber_44
28th April 2007, 16:18
I have the following code to paint a semi-transparent label on an image and then write on that label:


topLabel = new QTextEdit(imageLabel);
// topLabel->setAttribute(Qt::WA_NoSystemBackground);
QPalette palette;
// a white semi-transparent background
palette.setColor(QPalette::Window, QColor(255, 255, 255, 127));
topLabel->setPalette(palette);
topLabel->setText(*text);

It creates a white, opaque background.
It topLabel is QLabel, it creates a transparent background.
In any case, it is not semi-transparent.

marcel
28th April 2007, 17:52
I have the following code to paint a semi-transparent label on an image and then write on that label

I don't quite understand what you mean? A transparent label background?
Why don't you do this in the paintEvent of your label? Fill it with a semitransparent pixmap, and then draw on it whatever you want...

Regards.

ber_44
28th April 2007, 20:10
I mean a semi-transparent label background -- i.e. 50% white, 50% transparent.
According to the QWidget doc, "The background can be set using ... setPalette()."
So I'm hoping to avoid subclassing QStyle.
The size of the background can be changed, so a semi-transparent pixmap won't work.

marcel
28th April 2007, 20:25
You can always scale the pixmap, being transparent and all... And I didn't mean subclassing QStyle, but QLabel. I have tried this method before, and it works.

ber_44
28th April 2007, 20:35
Since it is only SEMI-transparent (that is, partially opaqe), scaling will give bad resolution.
Well, true that you can use a huge-huge pixmap and scale it down, but that's not very efficient.

marcel
28th April 2007, 21:05
Transparent pixmap, means only alpha channel, so no scaling effects occur.
Is this what you're trying to achieve?


void CTestLabel::paintEvent( QPaintEvent* )
{
QPainter p( this );
QPixmap pix( 1, 1 );
pix.fill( Qt::transparent );
p.drawPixmap( rect(), pix );
p.setOpacity( 0.5 );
p.fillRect( rect(), Qt::white );

p.setOpacity( 1 );
p.drawText( rect(), "test" );
};


I get a white label, 50% opaque, with some text on it.

jpn
29th April 2007, 00:01
palette.setColor(QPalette::Window, QColor(255, 255, 255, 127));


Shouldn't that be QPalette::Base (http://doc.trolltech.com/4.2/qpalette.html#ColorRole-enum): "Used as the background color for text entry widgets; usually white or another light color"?