title: PyQt5: Create semi-transparent window with non-transparent children
body: <p>I want to create a fullscreen window with semitransparent background, but fully visible children widgets (kind of overlay effect).</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>import sys
  
  from PyQt5.QtCore import *
  from PyQt5.QtGui import *
  from PyQt5.QtWidgets import *
  
  app = QApplication(sys.argv)
  
  # Create the main window
  window = QMainWindow()
  
  window.setWindowOpacity(0.3)
  window.setAttribute(Qt.WA_NoSystemBackground, True)
  window.setWindowFlags(Qt.FramelessWindowHint)
  
  # Create the button
  pushButton = QPushButton(window)
  pushButton.setGeometry(QRect(240, 190, 90, 31))
  pushButton.setText("Finished")
  pushButton.clicked.connect(app.quit)
  
  # Center the button
  qr = pushButton.frameGeometry()
  cp = QDesktopWidget().availableGeometry().center()
  qr.moveCenter(cp)
  pushButton.move(qr.topLeft())
  
  # Run the application
  window.showFullScreen()
  sys.exit(app.exec_())
  </code></pre>
  
  <p>This creates a semi-transparent effect, but even the button is semi-transparent.</p>
  
  <p>I also tried to substitute the</p>
  
  <pre><code>window.setWindowOpacity(0.3)
  </code></pre>
  
  <p>with this call</p>
  
  <pre><code>window.setAttribute(Qt.WA_TranslucentBackground, True)
  </code></pre>
  
  <p>but to no avail, in this case the background was <em>fully transparent</em> (while the button was correctly fully visible).</p>
  
  <p><strong>Solution: (implemented thanks to Aaron's suggestion)</strong>:</p>
  
  <p>The trick is in implementing a custom paintEvent for the main window.</p>
  
  <pre><code>import sys
  
  from PyQt5.QtCore import *
  from PyQt5.QtGui import *
  from PyQt5.QtWidgets import *
  
  class CustomWindow(QMainWindow):
      def paintEvent(self, event=None):
          painter = QPainter(self)
  
          painter.setOpacity(0.7)
          painter.setBrush(Qt.white)
          painter.setPen(QPen(Qt.white))   
          painter.drawRect(self.rect())
  
  
  app = QApplication(sys.argv)
  
  # Create the main window
  window = CustomWindow()
  
  window.setWindowFlags(Qt.FramelessWindowHint)
  window.setAttribute(Qt.WA_NoSystemBackground, True)
  window.setAttribute(Qt.WA_TranslucentBackground, True)
  
  # Create the button
  pushButton = QPushButton(window)
  pushButton.setGeometry(QRect(240, 190, 90, 31))
  pushButton.setText("Finished")
  pushButton.clicked.connect(app.quit)
  
  # Center the button
  qr = pushButton.frameGeometry()
  cp = QDesktopWidget().availableGeometry().center()
  qr.moveCenter(cp)
  pushButton.move(qr.topLeft())
  
  # Run the application
  window.showFullScreen()
  sys.exit(app.exec_())
  </code></pre>