PDA

View Full Version : Transparent background on QLabel on transparent QWidget



codeslicer
12th February 2008, 23:38
Yes, I know. I read all of the threads which had to do with this, but I couldn't extract any information which could have been helpful to me.

Here's what I need:
I am trying to create a custom QWidget which doesn't use the default system frame, but instead its own. The frame is irregularly shaped, and I want to use setMask() (http://doc.trolltech.com/latest/setmask%28%29.html) to show only the opaque parts of the QWidget "Dialog". Now, everything is fine, the dialog is transparent as it should, but the images aren't. I have png images which use transparency and on the dialog, the system background shows up, which makes it not so transparent after all. I tried using:

labelWithImage->setMask(QPixmap("/app/labelImage.png").mask());but nothing happened. Then I tried setting the Qt::WA_NoSystemBackground, or Qt::WA_ContentsPropagated, but that resulted in weird results. I know this is possible because I saw some people do it in this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-ugly-move-with-a-top-level-setmasked-widget-10825.html). Look at the apple image on the first post. Yes it's ugly but how did they implement it? They just didn't include how to do it :(
http://www.qtcentre.org/forum/attachment.php?attachmentid=480&d=1153221757
An example:
As you can see, the background of the label isn't transparent. Now I have an image where the transparent pixels show up as that color. How would I solve this?

codeslicer
13th February 2008, 02:10
... No one even replied here. But I finally solved it using:

void resizeEvent(QResizeEvent *e){
QDialog::resizeEvent(e);
//label_2->setAttribute(Qt::WA_ContentsPropagated );
QPixmap pixmap(":/new/prefix1/stand.png");
label_2->setPixmap(pixmap);
label_2->setMask(pixmap.mask());
QRegion reg(frameGeometry());
reg-=QRegion(geometry());
reg+=childrenRegion();
setMask(reg);
}

Also, I used 2 horizontal spacers to center my object in the widget. Otherwise the Mask would cover the wrong part of the widget. But I have a question. My widget is a rectangle with 4 "fixed" size corners and then "expanding" size spacers, which resize depending on the window. When I resize it it seems to flicker a lot, maybe because it's an irregular shape. (There is this "stand" on the bottom which makes it look like commonly used electronic gadget) So basically it's not exactly a rectangle. And how do I make a size grip on the irregular object? Thanks for all the helpful people like wysota and marcel who helped me with my previous problems. :)