-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgui_qt_embed.py
67 lines (47 loc) · 1.73 KB
/
gui_qt_embed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
An example demonstrating a qt app with a wgpu viz inside.
If needed, change the PySide6 import to e.g. PyQt6, PyQt5, or PySide2.
"""
# ruff: noqa: N802
# run_example = false
import time
import importlib
from triangle import setup_drawing_sync
# For the sake of making this example Just Work, we try multiple QT libs
for lib in ("PySide6", "PyQt6", "PySide2", "PyQt5"):
try:
QtWidgets = importlib.import_module(".QtWidgets", lib)
break
except ModuleNotFoundError:
pass
from wgpu.gui.qt import WgpuWidget # noqa: E402
class ExampleWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.resize(640, 480)
self.setWindowTitle("wgpu triangle embedded in a qt app")
splitter = QtWidgets.QSplitter()
self.button = QtWidgets.QPushButton("Hello world", self)
self.canvas = WgpuWidget(splitter)
self.output = QtWidgets.QTextEdit(splitter)
self.button.clicked.connect(self.whenButtonClicked)
splitter.addWidget(self.canvas)
splitter.addWidget(self.output)
splitter.setSizes([400, 300])
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.button, 0)
layout.addWidget(splitter, 1)
self.setLayout(layout)
self.show()
def addLine(self, line):
t = self.output.toPlainText()
t += "\n" + line
self.output.setPlainText(t)
def whenButtonClicked(self):
self.addLine(f"Clicked at {time.time():0.1f}")
app = QtWidgets.QApplication([])
example = ExampleWidget()
draw_frame = setup_drawing_sync(example.canvas)
example.canvas.request_draw(draw_frame)
# Enter Qt event loop (compatible with qt5/qt6)
app.exec() if hasattr(app, "exec") else app.exec_()