提问人:dukeeloo 提问时间:9/3/2023 更新时间:9/3/2023 访问量:46
初始化QWebEngineView时如何避免白闪?
How to avoid white flash when initializing QWebEngineView?
问:
我有一个 QWebEngineView,在我的应用程序初始化时会填充它。
第一次调用时,整个应用程序闪烁白色,而不仅仅是 QWebEngineView 的区域。
我试图尽可能长时间地延迟调用,但之后必须执行其他代码,导致闪存持续几秒钟。
当应用程序处于深色模式时,白色尤其令人不快。
下面的示例应该允许在单击按钮时重现效果。
请注意,第二次单击该按钮时,闪光灯会像预期的那样限制在 QWebEngineView 区域。setHtml
setHtml
有没有办法避免在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())
答: 暂无答案
评论
dummy = QWebEngineView(parent=self); dummy.setHtml('dummy');dummy.deleteLater()