I want to show a png file contained alpha channel in a top window properly.
-
How to do that?
I am using win7 + qt 5.1.1.
When the window is show, i want to see the desktop if the png's pixel's alpha value is 0. -
Have a look at the following code "snippet for setting semi-transparent background color of a QWidget":http://qt-project.org/wiki/QWidget_Semi-transparent_Background_Color. Class QMainWindow also contains method paintEvent so you do an overriding just like in the example for QWidget. As you can see the code snippet uses QColor and QPainter. Adjust the transparency using method setAlpha which value is in range 0 to 255. A png can be drawn using method "drawImage of class QPainter":http://qt-project.org/doc/qt-5.0/qtgui/qpainter.html#drawImage
As "an alternative solution may this example using WA_TranslucentBackground":http://www.codeprogress.com/cpp/libraries/qt/showQtExample.php?index=191&key=QMainWindowTransparentBg might be useful too.
-
Now, I know I am asking a wrong question.
For this question, the answer code
@
int main() {
/* Show transparent picture in a top window */
QImage image(":/images/assdqw.png");
QPainter p;
p.begin(&image);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.fillRect(image.rect(), QColor(0, 0, 0, 255));
p.end();QLabel *l = new QLabel(NULL, Qt::FramelessWindowHint); l->setAttribute(Qt::WA_TranslucentBackground, true); l->setPixmap(QPixmap::fromImage(image)); l->show();
}
@