From 940b9b0d257301734c600078bab123da3421786c Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Fri, 12 Apr 2024 21:32:02 -0400 Subject: [PATCH] update qt examples --- examples/qt/{video.py => embed.py} | 27 ++++++++++++--------------- examples/qt/imagewidget.py | 15 +++++++++------ examples/qt/minimal.py | 29 +++++++++-------------------- 3 files changed, 30 insertions(+), 41 deletions(-) rename examples/qt/{video.py => embed.py} (54%) diff --git a/examples/qt/video.py b/examples/qt/embed.py similarity index 54% rename from examples/qt/video.py rename to examples/qt/embed.py index 9fd77a999..a3b156021 100644 --- a/examples/qt/video.py +++ b/examples/qt/embed.py @@ -5,28 +5,22 @@ import fastplotlib as fpl import imageio.v3 as iio -# Qt app MUST be instantiated before creating any fpl objects, or any other Qt objects -app = QtWidgets.QApplication([]) video = iio.imread("imageio:cockatoo.mp4") -# force qt canvas, wgpu will sometimes pick glfw by default even if Qt is present -plot = fpl.Plot(canvas="qt") +# fastplotlib and wgpu will auto-detect if Qt is imported and then use the Qt canvas and output context +fig = fpl.Figure() -plot.add_image(video[0], name="video") -plot.camera.local.scale *= -1 +fig[0, 0].add_image(video[0], name="video") def update_frame(ix): - plot["video"].data = video[ix] - # you can also do plot.graphics[0].data = video[ix] + fig[0, 0]["video"].data = video[ix] + # you can also do fig[0, 0].graphics[0].data = video[ix] -# create a QMainWindow, set the plot canvas as the main widget -# The canvas does not have to be in a QMainWindow and it does -# not have to be the central widget, it will work like any QWidget +# create a QMainWindow main_window = QtWidgets.QMainWindow() -main_window.setCentralWidget(plot.canvas) # Create a QSlider for updating frames slider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal) @@ -44,8 +38,11 @@ def update_frame(ix): dock ) -# calling plot.show() is required to start the rendering loop -plot.show() +# calling fig.show() is required to start the rendering loop +qwidget = fig.show() + +# set the qwidget as the central widget +main_window.setCentralWidget(qwidget) # set window size from width and height of video main_window.resize(video.shape[2], video.shape[1]) @@ -54,4 +51,4 @@ def update_frame(ix): main_window.show() # execute Qt app -app.exec() +fpl.run() diff --git a/examples/qt/imagewidget.py b/examples/qt/imagewidget.py index ab1a055f1..f82d082a0 100644 --- a/examples/qt/imagewidget.py +++ b/examples/qt/imagewidget.py @@ -4,14 +4,13 @@ import numpy as np from PyQt6 import QtWidgets import fastplotlib as fpl +import imageio.v3 as iio -# Qt app MUST be instantiated before creating any fpl objects, or any other Qt objects -app = QtWidgets.QApplication([]) images = np.random.rand(100, 512, 512) -# create image widget, force Qt canvas so it doesn't pick glfw -iw = fpl.ImageWidget(images, grid_plot_kwargs={"canvas": "qt"}) +# fastplotlib and wgpu will auto-detect if Qt is imported and then use the Qt canvas +iw = fpl.ImageWidget(images) iw.show() iw.widget.resize(800, 800) @@ -20,10 +19,14 @@ iw_mult = fpl.ImageWidget( images_list, - grid_plot_kwargs={"canvas": "qt"}, cmap="viridis" ) iw_mult.show() iw_mult.widget.resize(800, 800) -app.exec() +# image widget with rgb data +rgb_video = iio.imread("imageio:cockatoo.mp4") +iw_rgb = fpl.ImageWidget(rgb_video, rgb=[True]) +iw_rgb.show() + +fpl.run() diff --git a/examples/qt/minimal.py b/examples/qt/minimal.py index e4e5f6c2f..0d9009ba7 100644 --- a/examples/qt/minimal.py +++ b/examples/qt/minimal.py @@ -1,35 +1,24 @@ """ Minimal PyQt example that displays an image. Press "r" key to autoscale """ +# import Qt or PySide from PyQt6 import QtWidgets import fastplotlib as fpl import imageio.v3 as iio -# Qt app MUST be instantiated before creating any fpl objects, or any other Qt objects -app = QtWidgets.QApplication([]) - img = iio.imread("imageio:astronaut.png") -# force qt canvas, wgpu will sometimes pick glfw by default even if Qt is present -plot = fpl.Plot(canvas="qt") +# fastplotlib and wgpu will auto-detect if Qt is imported and then use the Qt canvas and Qt output context +fig = fpl.Figure() -plot.add_image(img) -plot.camera.local.scale *= -1 +fig[0, 0].add_image(img) -# must call plot.show() to start rendering loop -plot.show() +# must call fig.show() to start rendering loop and show the QWidget containing the fastplotlib figure +qwidget = fig.show() # set QWidget initial size from image width and height -plot.canvas.resize(*img.shape[:2]) - - -def autoscale(ev): - if ev.key == "r": - plot.auto_scale() - - -# useful if you pan/zoom away from the image -plot.renderer.add_event_handler(autoscale, "key_down") +qwidget.resize(*img.shape[:2]) # execute Qt app -app.exec() +# if this is part of a larger Qt QApplication, you can also call app.exec() where app is the QApplication instance +fpl.run()