关闭 WA_TranslucentBackground 会停止窗口重绘 [英] Turning WA_TranslucentBackground off stops window repainting

查看:39
本文介绍了关闭 WA_TranslucentBackground 会停止窗口重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我遇到的问题是,在我将 WA_TranslucentBackground 属性转回 false 后,在它为 true 之后,Window 将不再重绘,因此从那时起它仍然卡住显示相同的内容.有趣的是,点击事件仍然响应.

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

推荐答案

尝试替换 __init__ 中的 self.setAttribute(Qt.WA_TranslucentBackground, ...) 调用和toggle 使用以下方法.

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

在 PyQt-Py2.7-x86-gpl-4.9-1 (Windows 7) 上测试

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

这篇关于关闭 WA_TranslucentBackground 会停止窗口重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆