Skip to content

Commit 2b40220

Browse files
committed
working demo of widget using just an np buffer
1 parent f48f89a commit 2b40220

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

QtWidget/WebGPUWidget.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from abc import ABCMeta, abstractmethod
2+
from typing import List, Tuple
3+
24
import numpy as np
35
from qtpy.QtCore import QRect, Qt
46
from qtpy.QtGui import QColor, QFont, QImage, QPainter
57
from qtpy.QtWidgets import QWidget
68

7-
from typing import List, Optional, Tuple
8-
99

1010
class QWidgetABCMeta(type(QWidget), ABCMeta):
1111
"""
@@ -37,6 +37,7 @@ def __init__(self) -> None:
3737
super().__init__()
3838
self.initialized = False
3939
self.text_buffer: List[Tuple[int, int, str, int, str, QColor]] = []
40+
self.buffer = None
4041

4142
@abstractmethod
4243
def initializeWebGPU(self) -> None:
@@ -83,7 +84,7 @@ def paintEvent(self, event) -> None:
8384
self.paintWebGPU()
8485
painter = QPainter(self)
8586

86-
if hasattr(self, "buffer"):
87+
if self.buffer is not None:
8788
self._present_image(painter, self.buffer)
8889
for x, y, text, size, font, colour in self.text_buffer:
8990
painter.setPen(colour)

QtWidget/main.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env python3
2-
from abc import ABCMeta, abstractmethod
3-
from qtpy.QtWidgets import QWidget, QApplication
42
import sys
5-
from WebGPUWidget import WebGPUWidget
3+
4+
import numpy as np
65
from qtpy.QtCore import Qt
6+
from qtpy.QtWidgets import QApplication
7+
from WebGPUWidget import WebGPUWidget
78

89

910
class WebGPUScene(WebGPUWidget):
@@ -21,16 +22,19 @@ def initializeWebGPU(self) -> None:
2122
This method sets up the WebGPU context for the scene.
2223
"""
2324
print("initializeWebGPU")
25+
self.startTimer(100)
2426

2527
def paintWebGPU(self) -> None:
2628
"""
2729
Paint the WebGPU content.
2830
2931
This method renders the WebGPU content for the scene.
3032
"""
31-
print("paintWebGPU")
3233
self.render_text(10, 20, "PaintGPU Test", size=20, colour=Qt.yellow)
3334

35+
# going to randomly generate an np buff to fill the screen
36+
self.buffer = np.random.randint(0, 255, (1024, 720, 4), dtype=np.uint8)
37+
3438
def resizeWebGPU(self, width: int, height: int) -> None:
3539
"""
3640
Resize the WebGPU context.
@@ -43,6 +47,9 @@ def resizeWebGPU(self, width: int, height: int) -> None:
4347
"""
4448
print(f"resizeWebGPU {width} {height}")
4549

50+
def timerEvent(self, event):
51+
self.update()
52+
4653

4754
app = QApplication(sys.argv)
4855
win = WebGPUScene()

0 commit comments

Comments
 (0)