【问题标题】:Turning WA_TranslucentBackground off stops window repainting关闭 WA_TranslucentBackground 会停止窗口重绘
【发布时间】:2012-02-07 22:49:00
【问题描述】:

我有一个 PyQt4.9 窗口,我想在其中打开或关闭半透明。原因是它有时会显示一个全尺寸的声子视频控件,当设置 WA_TranslucentBackground 属性时它不起作用。 (由于 Qt 错误https://bugreports.qt.io/browse/QTBUG-8119

我遇到的问题是,在我将 WA_TranslucentBackground 属性设置回 false 后,当它为 true 后,Window 将不再重绘,因此从那时起它仍然卡在显示相同的东西。有趣的是,点击事件仍然响应。

下面是一些示例代码。单击增量按钮,它将更新按钮文本。单击切换按钮,然后再次单击增量按钮,更新不再显示。单击退出按钮关闭窗口,显示事件仍在响应。

如果有人有任何解决方案、变通办法或修复方法,我将不胜感激。谢谢。

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

【问题讨论】:

    标签: qt qt4 pyqt transparency pyqt4


    【解决方案1】:

    尝试用以下方法替换__init__toggle中的self.setAttribute(Qt.WA_TranslucentBackground, ...)调用。

    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-x86-gpl-4.9-1 (Windows 7) 上测试

    【讨论】:

      猜你喜欢
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多