PyQt5:使用非透明子项创建半透明 window

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

我想创建一个全屏 window 具有半透明背景,但子部件完全可见(一种叠加效果)。

这是我目前的情况:

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

这会创建一个半透明的效果,但即使是按钮也是半透明的。

我也试过替换

window.setWindowOpacity(0.3)

通过这次通话

window.setAttribute(Qt.WA_TranslucentBackground, True)

但无济于事,在这种情况下背景是 完全透明的(而按钮是完全可见的)。

解决方案:(感谢Aaron的建议实施)

诀窍在于为主要 window 实现自定义 paintEvent。

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

好的,虽然它似乎不适用于可用的标志,但您仍然可以使用 Qt.WA_TranslucentBackground 因为可以在该透明度上绘制半透明矩形。

从 QMainWindow 派生主窗口并使用 class 代替。

self.setAttribute(Qt.WA_TranslucentBackground, True)应用于class

像这样实现你的主窗口class的paintEvent(类似的,可能有错误,但原理应该有效):

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)

我只是想提供另一种解决方案,以防其他人遇到此问题。我的解决方法是这样的

首先将背景设置为完全透明。这仅适用于 window 的背景,不适用于子对象。

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

如果需要,您可以删除边框。

self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

现在,如果您仍希望有一些背景颜色,请将 QFrame 应用于您的 window,并将所有子对象放入其中。使用您的样式 sheet,像这样设置框架的颜色和您想要的不透明度。最后一个值是不透明度百分比。

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