Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 504ddcf

Browse filesBrowse files
authored
update qt examples (#485)
1 parent cc6dece commit 504ddcf
Copy full SHA for 504ddcf

File tree

3 files changed

+30
-41
lines changed
Filter options

3 files changed

+30
-41
lines changed

‎examples/qt/video.py renamed to ‎examples/qt/embed.py

Copy file name to clipboardExpand all lines: examples/qt/embed.py
+12-15Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,22 @@
55
import fastplotlib as fpl
66
import imageio.v3 as iio
77

8-
# Qt app MUST be instantiated before creating any fpl objects, or any other Qt objects
9-
app = QtWidgets.QApplication([])
108

119
video = iio.imread("imageio:cockatoo.mp4")
1210

13-
# force qt canvas, wgpu will sometimes pick glfw by default even if Qt is present
14-
plot = fpl.Plot(canvas="qt")
11+
# fastplotlib and wgpu will auto-detect if Qt is imported and then use the Qt canvas and output context
12+
fig = fpl.Figure()
1513

16-
plot.add_image(video[0], name="video")
17-
plot.camera.local.scale *= -1
14+
fig[0, 0].add_image(video[0], name="video")
1815

1916

2017
def update_frame(ix):
21-
plot["video"].data = video[ix]
22-
# you can also do plot.graphics[0].data = video[ix]
18+
fig[0, 0]["video"].data = video[ix]
19+
# you can also do fig[0, 0].graphics[0].data = video[ix]
2320

2421

25-
# create a QMainWindow, set the plot canvas as the main widget
26-
# The canvas does not have to be in a QMainWindow and it does
27-
# not have to be the central widget, it will work like any QWidget
22+
# create a QMainWindow
2823
main_window = QtWidgets.QMainWindow()
29-
main_window.setCentralWidget(plot.canvas)
3024

3125
# Create a QSlider for updating frames
3226
slider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
@@ -44,8 +38,11 @@ def update_frame(ix):
4438
dock
4539
)
4640

47-
# calling plot.show() is required to start the rendering loop
48-
plot.show()
41+
# calling fig.show() is required to start the rendering loop
42+
qwidget = fig.show()
43+
44+
# set the qwidget as the central widget
45+
main_window.setCentralWidget(qwidget)
4946

5047
# set window size from width and height of video
5148
main_window.resize(video.shape[2], video.shape[1])
@@ -54,4 +51,4 @@ def update_frame(ix):
5451
main_window.show()
5552

5653
# execute Qt app
57-
app.exec()
54+
fpl.run()

‎examples/qt/imagewidget.py

Copy file name to clipboardExpand all lines: examples/qt/imagewidget.py
+9-6Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import numpy as np
55
from PyQt6 import QtWidgets
66
import fastplotlib as fpl
7+
import imageio.v3 as iio
78

8-
# Qt app MUST be instantiated before creating any fpl objects, or any other Qt objects
9-
app = QtWidgets.QApplication([])
109

1110
images = np.random.rand(100, 512, 512)
1211

13-
# create image widget, force Qt canvas so it doesn't pick glfw
14-
iw = fpl.ImageWidget(images, grid_plot_kwargs={"canvas": "qt"})
12+
# fastplotlib and wgpu will auto-detect if Qt is imported and then use the Qt canvas
13+
iw = fpl.ImageWidget(images)
1514
iw.show()
1615
iw.widget.resize(800, 800)
1716

@@ -20,10 +19,14 @@
2019

2120
iw_mult = fpl.ImageWidget(
2221
images_list,
23-
grid_plot_kwargs={"canvas": "qt"},
2422
cmap="viridis"
2523
)
2624
iw_mult.show()
2725
iw_mult.widget.resize(800, 800)
2826

29-
app.exec()
27+
# image widget with rgb data
28+
rgb_video = iio.imread("imageio:cockatoo.mp4")
29+
iw_rgb = fpl.ImageWidget(rgb_video, rgb=[True])
30+
iw_rgb.show()
31+
32+
fpl.run()

‎examples/qt/minimal.py

Copy file name to clipboard
+9-20Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
"""
22
Minimal PyQt example that displays an image. Press "r" key to autoscale
33
"""
4+
# import Qt or PySide
45
from PyQt6 import QtWidgets
56
import fastplotlib as fpl
67
import imageio.v3 as iio
78

8-
# Qt app MUST be instantiated before creating any fpl objects, or any other Qt objects
9-
app = QtWidgets.QApplication([])
10-
119
img = iio.imread("imageio:astronaut.png")
1210

13-
# force qt canvas, wgpu will sometimes pick glfw by default even if Qt is present
14-
plot = fpl.Plot(canvas="qt")
11+
# fastplotlib and wgpu will auto-detect if Qt is imported and then use the Qt canvas and Qt output context
12+
fig = fpl.Figure()
1513

16-
plot.add_image(img)
17-
plot.camera.local.scale *= -1
14+
fig[0, 0].add_image(img)
1815

19-
# must call plot.show() to start rendering loop
20-
plot.show()
16+
# must call fig.show() to start rendering loop and show the QWidget containing the fastplotlib figure
17+
qwidget = fig.show()
2118

2219
# set QWidget initial size from image width and height
23-
plot.canvas.resize(*img.shape[:2])
24-
25-
26-
def autoscale(ev):
27-
if ev.key == "r":
28-
plot.auto_scale()
29-
30-
31-
# useful if you pan/zoom away from the image
32-
plot.renderer.add_event_handler(autoscale, "key_down")
20+
qwidget.resize(*img.shape[:2])
3321

3422
# execute Qt app
35-
app.exec()
23+
# if this is part of a larger Qt QApplication, you can also call app.exec() where app is the QApplication instance
24+
fpl.run()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.