PyQt5: Create semi-transparent window with non-transparent children PyQt5: Create semi-transparent window with non-transparent children python python

PyQt5: Create semi-transparent window with non-transparent children


Ok, while is seems not to work with the available flags you can still use Qt.WA_TranslucentBackground because it is possible to draw a semitranparent rect on that transparency.

Derive your mainwindow from QMainWindow and use that class instead.

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

Implement the paintEvent of your mainwindow class like this (similar, might 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 canvasp.setOpacity(0.3)p.setBrush(QBrush(Qt.white)) # use the color you likep.setPen(QPen(Qt.transparen))p.drawRect(rect()) # draws the canvas with desired opacityp.start(self)      # now draw on the window itselfp.drawPixmap(rect(), canvas)


I just wanted to provide another solution in case someone else runs into this problem. The way I solved it is like this.

First set your background to be completely transparent. This only applies to the window's background, and not the children objects.

self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)

You can remove the border if you want with this.

self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

Now, if you want there to still be some background color, apply a QFrame to your window, and place all child objects within it. Using your style sheet, set the color of the frame and your desired opacity like this. The last value is the opacity percentage.

self.main_frame.setStyleSheet("background-color: rgba(0, 120, 185, 60)")