半透明の背景を持つフルスクリーンのウィンドウを作成したいが、完全に見える子ウィジェット(オーバーレイ効果のようなもの)を作成したい。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_())
これは、半透明の効果を作成しますが、でもボタンが半透明です。
Iはまた、この呼び出し
window.setAttribute(Qt.WA_TranslucentBackground, True)
と
window.setWindowOpacity(0.3)
を置換しようとしたが、ボタンが正しく完全に可視であった無駄に、この場合には背景が(完全に透明でした)。
ソリューション:(アーロンの提案のおかげで実装):
トリックは、メインウィンドウのカスタム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_())
何が起こることはボタンがまだあるということです半透明で、バックグラウンドはシステム定義の色に設定されます(黒ではなく、白の色です)。私はsetWindowOpacityが非ウィンドウウィジェットで動作するのかどうか分かりません。 – Enuy
私の答えを編集しました。私はこれを時々好きにしたし、いつも必要なときに働いた。 – Aaron
さて、私はこのQT擬似コードをPyQTに変換し、これがどのように機能するかを見てみましょう。 – Enuy