2012-01-11 76 views
3

我有一個PyQt4.9窗口,我希望打開或關閉半透明窗口。原因在於它有時會顯示全尺寸聲子視頻控件,它在設置WA_TranslucentBackground屬性時不起作用。 (由於Qt錯誤https://bugreports.qt.io/browse/QTBUG-8119將WA_TranslucentBackground關閉停止窗口重新繪製

我遇到的問題是,在WA_TranslucentBackground屬性返回false後,窗口將不再重繪,所以它仍然顯示與此相同的東西指向。有趣的是,點擊事件仍然響應。

下面是一些示例代碼。點擊增加按鈕,它將更新按鈕文本。點擊切換按鈕,然後再次點擊增量按鈕,不再顯示更新。點擊退出按鈕關閉窗口,顯示事件仍在響應。

如果任何人有任何解決方案,解決方法或修復,我會很感激他們。謝謝。

import sys 

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

class Settings(QWidget): 

    def __init__(self, desktop):  
     QWidget.__init__(self) 
     self.setAttribute(Qt.WA_TranslucentBackground, True) 
     self.setWindowFlags(Qt.FramelessWindowHint) 
     self.istransparent = True 
     self.count = 0 
     self.setWindowTitle("Transparent") 
     self.resize(300, 150) 
     self.incr_button = QPushButton("Increment") 
     toggle_button = QPushButton("Toggle Transparency") 
     exit_button = QPushButton("Exit") 
     grid = QGridLayout() 
     grid.addWidget(self.incr_button, 0, 0) 
     grid.addWidget(toggle_button, 1, 0) 
     grid.addWidget(exit_button, 2, 0) 
     self.setLayout(grid)   
     self.connect(toggle_button, SIGNAL("clicked()"), self.toggle) 
     self.connect(self.incr_button, SIGNAL("clicked()"), self.increment) 
     self.connect(exit_button, SIGNAL("clicked()"), self.close) 

    def increment(self): 
     self.count = self.count + 1 
     self.incr_button.setText("Increment (%i)" % self.count) 

    def toggle(self): 
     self.istransparent = not self.istransparent 
     self.setAttribute(Qt.WA_TranslucentBackground, self.istransparent) 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    s = Settings(app.desktop()) 
    s.show() 
    sys.exit(app.exec_()) 

回答

2

嘗試更換在__init__self.setAttribute(Qt.WA_TranslucentBackground, ...)電話和toggle用下面的方法。

def set_transparency(self, enabled): 
    if enabled: 
     self.setAutoFillBackground(False) 
    else: 
     self.setAttribute(Qt.WA_NoSystemBackground, False) 

    self.setAttribute(Qt.WA_TranslucentBackground, enabled) 
    self.repaint() 

測試上的PyQt-Py2.7 86 GPL-4.9-1(視窗7)