Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

qt - Turning WA_TranslucentBackground off stops window repainting

I have a PyQt4.9 window where I would like to toggle the translucency on or off. The reason being is that it sometimes shows a full size phonon video control which doesn't work when the WA_TranslucentBackground attribute is set. (Due to a Qt bug https://bugreports.qt.io/browse/QTBUG-8119)

The problem I have is, after I turn WA_TranslucentBackground attribute back to false, after it has been true, the Window will no longer redraw, so it remains stuck showing the same thing from that point on. Interestingly, click events still respond.

Some example code follows. Click the increment button, and it will update the button text. Click the toggle button and then click the increment button again, and updates no longer show. Clicking the exit button closes the window, showing the events are still responding.

If anyone has any solutions, workarounds or fixes I'd appreciate them. Thanks.

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_())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to replace self.setAttribute(Qt.WA_TranslucentBackground, ...) calls in __init__ and toggle with following method.

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

Tested on PyQt-Py2.7-x86-gpl-4.9-1 (Windows 7)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...