2015-11-29 28 views
6

半透明の背景を持つフルスクリーンのウィンドウを作成したいが、完全に見える子ウィジェット(オーバーレイ効果のようなもの)を作成したい。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_()) 

答えて

4

[OK]を、ISは、その透明性にsemitranparent矩形を描画することができるので、あなたはまだQt.WA_TranslucentBackgroundを使用することができます使用可能なフラグでは動作しないようですしながら。

メインウィンドウをQMainWindowから派生させ、代わりにそのクラスを使用します。

このようなあなたのメイン・ウィンドウクラスのpaintEventを(類似した、エラーが含まれているかもしれませんが、原理は動作するはずです)を実装し、そのクラスに

self.setAttribute(Qt.WA_TranslucentBackground, True)を適用します。

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) 
+0

何が起こることはボタンがまだあるということです半透明で、バックグラウンドはシステム定義の色に設定されます(黒ではなく、白の色です)。私はsetWindowOpacityが非ウィンドウウィジェットで動作するのかどうか分かりません。 – Enuy

+0

私の答えを編集しました。私はこれを時々好きにしたし、いつも必要なときに働いた。 – Aaron

+0

さて、私はこのQT擬似コードをPyQTに変換し、これがどのように機能するかを見てみましょう。 – Enuy

関連する問題