PyQt5:使用不透明的子项创建半透明窗口

时间:2015-11-29 11:17:31

标签: python qt pyqt qt5 pyqt5

我想创建一个半透明背景的全屏窗口,但是完全可见的子窗口小部件(覆盖效果的种类)。

这是我到目前为止所拥有的:

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的建议)

诀窍在于为主窗口实现自定义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_())

2 个答案:

答案 0 :(得分:6)

好的,虽然似乎无法使用可用的标志,但你仍然可以使用Qt.WA_TranslucentBackground,因为可以在该透明度上绘制一个半透明的矩形。

从QMainWindow派生您的主窗口并改为使用该类。

self.setAttribute(Qt.WA_TranslucentBackground, True)应用于该班级

像这样实现mainwindow类的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)

答案 1 :(得分:0)

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

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

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

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

self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

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

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