初始化QWebEngineView时如何避免白闪?

How to avoid white flash when initializing QWebEngineView?

提问人:dukeeloo 提问时间:9/3/2023 更新时间:9/3/2023 访问量:46

问:

我有一个 QWebEngineView,在我的应用程序初始化时会填充它。 第一次调用时,整个应用程序闪烁白色,而不仅仅是 QWebEngineView 的区域。 我试图尽可能长时间地延迟调用,但之后必须执行其他代码,导致闪存持续几秒钟。 当应用程序处于深色模式时,白色尤其令人不快。 下面的示例应该允许在单击按钮时重现效果。 请注意,第二次单击该按钮时,闪光灯会像预期的那样限制在 QWebEngineView 区域。setHtmlsetHtml

有没有办法避免在QWebEngineView初始化之前更新GUI,或者至少更改更新期间看到的颜色?

import sys
import time
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from PyQt6 import QtWebEngineWidgets
from PyQt6.QtWebEngineWidgets import QWebEngineView

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setGeometry(100, 100, 400, 400)
        self.setStyleSheet('background:gray')
        self.statusBar().showMessage("Status Bar Message disappears while initializing first webEngineView")   
        self.lay = QVBoxLayout()       
        w = QWidget()
        w.setLayout(self.lay)
        self.setCentralWidget(w)
        self.b = QPushButton('Add webEngineView')
        self.b.setStyleSheet('background:black;color:white')
        self.b.clicked.connect(self.load)
        self.lay.addWidget(self.b)          
        
    def load(self):
        self.webEngineView = QWebEngineView(parent=self)
        self.lay.addWidget(self.webEngineView)        
        # self.setUpdatesEnabled(False) # does not help
        self.webEngineView.setHtml("<style> body { background-color: black; color: white; } </style> <h1>Heading</h1> <p>Paragraph</p>") 
        # QApplication.processEvents() # reduces flash duration in this example but not in the full application
        time.sleep(1) # represents other code for GUI initialization
        # self.setUpdatesEnabled(True)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec())
python pyqt6 qwebengine查看

评论

1赞 musicamante 9/3/2023
您可能对此无能为力,这可能是小部件渲染中“切换”到 GL 兼容模式的更改所必需的。而且,显然,您无法控制颜色,它可能是操作系统或图形驱动程序的默认“齐平”颜色。但是,您可以尝试从窗口创建一开始就添加视图(作为它自己的子视图!)并立即隐藏它,然后仅在必要时显示它。
0赞 dukeeloo 9/4/2023
谢谢你@musicamante!我已添加到我的应用程序的开头,并使用计时器触发初始化的其余部分。不再有闪光灯。dummy = QWebEngineView(parent=self); dummy.setHtml('dummy');dummy.deleteLater()

答: 暂无答案