Skip to content

Instantly share code, notes, and snippets.

@8Observer8
Created September 6, 2024 09:26
Show Gist options
  • Save 8Observer8/ce0e0c1d763158403f333e2b1cb8411b to your computer and use it in GitHub Desktop.
Save 8Observer8/ce0e0c1d763158403f333e2b1cb8411b to your computer and use it in GitHub Desktop.
How to enable WebGL in QWebEngineView
import sys
import os
from PyQt6.QtCore import QUrl
from PyQt6.QtWebEngineCore import QWebEngineSettings
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWidgets import QApplication, QVBoxLayout, QWidget
class Widget(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QWebEngineView, PyQt6, Python")
self.resize(650, 400)
layout = QVBoxLayout()
view = QWebEngineView()
layout.addWidget(view)
self.setLayout(layout)
# view.load(QUrl("file:///web-project/index.html"))
view.settings().setAttribute(QWebEngineSettings.WebAttribute.WebGLEnabled, True)
print("----------------------")
print(view.settings().testAttribute(QWebEngineSettings.WebAttribute.WebGLEnabled))
view.setUrl(QUrl("https://get.webgl.org/"))
if __name__ == "__main__":
os.environ['QTWEBENGINE_CHROMIUM_FLAGS'] = '--use-gl=angle --gpu --gpu-launcher --in-process-gpu --ignore-gpu-blacklist --ignore-gpu-blocklist'
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec())
@8Observer8
Copy link
Author

8Observer8 commented Sep 6, 2024

Topic on StackOverflow

Result for PyQt 6.7.0. It doesn't work:

image

Console log:

D3D11 smoke test: Failed to create vertex shader
----------------------
True
D3D11 smoke test: Failed to create vertex shader
js: Unrecognized Content-Security-Policy directive 'image-src'.

But when I have deleted everything that is related to PyQt 6.7.0 and PyQt6-WebEngine and I have installed PyQt 6.7.1 it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment