PDA

View Full Version : QImage and Qt::transparent



babu198649
15th April 2008, 14:06
hi
i want to paint on an image.How to make the image background as transparent.

the following code can change the background color of the image .


QColormap colormap=QColormap::instance();
image.fill(colormap.pixel(Qt::green));

but it fails to make the background transparent even when the code is changed to

QColormap colormap=QColormap::instance();
image.fill(colormap.pixel(Qt::transparent));

i wnt to paint on aQImage rather than QPixmap is because, i am painting in a multithreaded program.

how to make the background of a QImage as transparent.

marcel
15th April 2008, 14:15
try:

image.fill(qRgba(0, 0, 0, 255));

and make sure the QImage depth is 32 bits.

babu198649
15th April 2008, 14:22
The image depth is 32 but had not worked:(.


image = QImage(1000,1000, QImage::Format_RGB32);
qDebug()<<"depth "<<image.depth(); //prints 32
image.fill(qRgba(0, 0, 0, 255));

marcel
15th April 2008, 14:30
how do you test if the image is transparent or not?

babu198649
15th April 2008, 14:44
after the above code

//code in worker thread

QPainter painter(&image);
painter.save();
//painting is performed
painter.restore();


//code in main gui thread.

pixmap=QPixmap::fromImage(worker_thread.image);//image is converted to pixmap
update();//inside the paintevent the pixmap is painted on to widget.

i get black as the background color.

But if any color is given(instead of Qt::transparent) such as green then green color is the background color
image.fill(colormap.pixel(Qt::green));

aamer4yu
15th April 2008, 20:46
what are u trying to achieve ? I am still not clear abt it .

Are u making an application by which user can draw over a area, with an image in the bcground ??
Sort of image editing application ? am i getting u right ?

babu198649
16th April 2008, 08:51
Sort of image editing application ?
no

A image is constructed(made) by painting on a QImage object with QPainter .Before painting on the image the image background should be set to transparent.

aamer4yu
16th April 2008, 09:08
ohh oh !!!
I guess i caught the mistake...


image.fill(qRgba(0, 0, 0, 255)); should be
image.fill(qRgba(0, 0, 0, 0));

255 specifies NO OPACITY !!

Hope this will solve the prob ;)

babu198649
16th April 2008, 09:11
thanks for reply

but it did not work:(

wysota
16th April 2008, 09:21
QImage::Format_RGB32 doesn't support transparency. Use QImage::Format_ARGB32

babu198649
16th April 2008, 09:28
thanks wysota
constructing the image with QImage::Format_ARGB32 and then using image.fill(qRgba(0,0,0,0));
makes the image transparent

Cupidvogel
13th February 2016, 19:51
Hi, sorry to bump this thread, but is it possible to make a QIcon out of an image where some parts are transparent, and then fill the opaque parts of the icon with some background color?