How can I have a partially transparent pixmap on my toplevel window?

For top level windows we only support constant transparency and masking, so to achieve this effect, you can do one out of two things:

  1. Extract the transparent areas from the image as a bitmap and set this as the widget mask. This will not allow you to have partially transparent areas.
  2. If you want to show a QPixmap [doc.qt.nokia.com] with an alpha as your splash screen, then you
    can use this approach which is slightly less efficient. You need to figure out the geometry of your splashscreen and use QPixmap::grabWindow() [doc.qt.nokia.com] to fetch this area into a QPixmap. Then take your pixmap and blend it on top of the grabbed pixmap. The resulting pixmap you can set as the widget pixmap.

  1. #include <QtGui>
  2.  
  3. class Widget : public QWidget
  4. {
  5. public:
  6.   Widget()
  7.   {
  8.     setWindowFlags(Qt::FramelessWindowHint);
  9.     transparentPixmap = QPixmap("pixmap with an alpha");
  10.     desktopBackground= QPixmap::grabWindow(0, x(), y(), width(),height());
  11.   }
  12.   void paintEvent(QPaintEvent *event)
  13.   {
  14.     QPainter painter(this);
  15.     painter.drawPixmap(0, 0, desktopBackground);
  16.     painter.drawPixmap(0,0, transparentPixmap );
  17.   }
  18.   void resizeEvent(QResizeEvent *event)
  19.   {
  20.     setWindowOpacity(0.0);
  21.     desktopBackground= QPixmap::grabWindow(0, x(), y(), width(),height());
  22.     setWindowOpacity(1.0);
  23.     update();
  24.   }
  25. private:
  26.   QPixmap desktopBackground;
  27.   QPixmap transparentPixmap;
  28. };
  29. int main(int argc, char **argv)
  30. {
  31.   QApplication app(argc, argv);
  32.   Widget box;
  33.   box.resize(255, 255);
  34.   box.show();
  35.   return app.exec();
  36. }
  37.  

No comments

Write a comment

Sorry, you must be logged in to post a comment.