PyQt5: create a translucent window with opaque children

I want to create a full-screen window with a translucent background, but fully visible child widgets (overlay effect view).

Here is what I still have:

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_()) 

This creates a translucent effect, but even the button is translucent.

I also tried to substitute

 window.setWindowOpacity(0.3) 

with this challenge

 window.setAttribute(Qt.WA_TranslucentBackground, True) 

but to no avail, in this case the background was completely transparent (while the button was completely fully visible).

Solution: (implemented thanks to Aaron’s proposal) :

The trick is to implement a custom paintEvent for the main window.

 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_()) 
+6
source share
1 answer

Well, although it doesn't seem to work with the available flags, you can still use Qt.WA_TranslucentBackground , because you can make a translucent rectangle of this transparency.

Derive the main title from QMainWindow and use this class.

Apply self.setAttribute(Qt.WA_TranslucentBackground, True) to this class

Deploy the paintEvent of your mainwindow class like this (it may seem to contain errors, but the principle should work):

 QPixmap canvas(rect()) canvas.fill(Qt.transparent) # fill transparent (makes alpha channel available) QPainter p(canvas) # draw on the canvas p.setOpacity(0.3) p.setBrush(QBrush(Qt.white)) # use the color you like p.setPen(QPen(Qt.transparen)) p.drawRect(rect()) # draws the canvas with desired opacity p.start(self) # now draw on the window itself p.drawPixmap(rect(), canvas) 
+4
source

All Articles