qtshadowqmainwindow

How to set the shadow around the window


I need to show shadow around my mainWindow and I cannot set WA_TranslucentBackground on it as I need to show a video.

How do I set a shadow for QMainWindow without making it transparent?


Solution

  • You need to :

    1. Create top level QWidget

    2. Make it translucent and frameless

      setWindowFlags(Qt::FramelessWindowHint);    
      setAttribute(Qt::WA_TranslucentBackground);
      
    3. Insert your MainWindow into created widget. Left some margins for shadow (about 5-15 px)

    4. Add QGraphicsDropShadowEffect to MainWindow:

      QGraphicsDropShadowEffect *wndShadow = new QGraphicsDropShadowEffect;
      wndShadow->setBlurRadius(9.0);
      wndShadow->setColor(QColor(0, 0, 0, 160));
      wndShadow->setOffset(4.0);
      mainWindow->setGraphicsEffect(wndShadow);
      

    Result:

    A widget with drop shadow