[Qgis-user] problem with matplotlib in context with QGIS processing plugins
Johannes Kröger (WhereGroup)
johannes.kroeger at wheregroup.com
Wed Feb 15 00:39:22 PST 2023
Hi Manfred,
not what you asked but: Are you sure you need a second QMainWindow or
maybe a non-modal QDialog or a QDockWidget would be sufficient? Those
might be easier to use. Or maybe even just using the window that
plt.show() creates?
Cheers, Hannes
Am 14.02.23 um 22:11 schrieb Manfred Strahlhofer via QGIS-User:
> Hello!
>
> There is a problem when using "matplotlib.backends" and drawing plots
> when called from a QGIS processing plugin. I am using the following
> test code:
> # sample code below draws a canvas with a plot
> # however closing this plot canvas crashes the plugin (cannot be
> restarted)
> # and further qgis can't be closed with the X button
> # originates from:
> https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py
> importsys
> importtime
> importnumpyasnp
> frommatplotlib.backends.qt_compatimportQtWidgets
> frommatplotlib.backends.backend_qtaggimport(
> FigureCanvas, NavigationToolbar2QT asNavigationToolbar)
> frommatplotlib.figureimportFigure
> classApplicationWindow(QtWidgets.QMainWindow):
> def__init__(self):
> super().__init__()
> self._main= QtWidgets.QWidget()
> self.setCentralWidget(self._main)
> layout= QtWidgets.QVBoxLayout(self._main)
> static_canvas= FigureCanvas(Figure(figsize=(5, 3)))
> # Ideally one would use self.addToolBar here, but it is slightly
> # incompatible between PyQt6 and other bindings, so we just add the
> # toolbar as a plain widget instead.
> layout.addWidget(NavigationToolbar(static_canvas, self))
> layout.addWidget(static_canvas)
> dynamic_canvas= FigureCanvas(Figure(figsize=(5, 3)))
> layout.addWidget(dynamic_canvas)
> layout.addWidget(NavigationToolbar(dynamic_canvas, self))
> self._static_ax= static_canvas.figure.subplots()
> t= np.linspace(0, 10, 501)
> self._static_ax.plot(t, np.tan(t), ".")
> self._dynamic_ax= dynamic_canvas.figure.subplots()
> t= np.linspace(0, 10, 101)
> # Set up a Line2D.
> self._line, = self._dynamic_ax.plot(t, np.sin(t+ time.time()))
> self._timer= dynamic_canvas.new_timer(50)
> self._timer.add_callback(self._update_canvas)
> self._timer.start()
> def_update_canvas(self):
> t= np.linspace(0, 10, 101)
> # Shift the sinusoid as a function of time.
> self._line.set_data(t, np.sin(t+ time.time()))
> self._line.figure.canvas.draw()
> deftest_plot():
> # Check whether there is already a running QApplication (e.g., if running
> # from an IDE).
> #qapp = QtWidgets.QApplication.instance() this stucks QGIS!
> #if not qapp:
> qapp= QtWidgets.QApplication(sys.argv) # this stucks QGIS main window
> when the dialog's close button is pressed
> app= ApplicationWindow()
> app.show()
> app.activateWindow()
> app.raise_()
> qapp.exec()
> #app.exit()
>
> When using "qapp = QtWidgets.QApplication.instance()", QGIS stucks
> completely and no plot is shown ever.
> When using "qapp = QtWidgets.QApplication(sys.argv)", the plot is
> shown and updated correctly. But when pressing the "close button" of
> the plot window, there are some strange effects (cannot start a plugin
> again) and the "close button" of the QGIS main window is disabled.
> Have to shut down qgis-bin.exe from the windows task-manager.
> I am using:
> QGIS Version 3.24.1 Tisler
> matplotlib 3.5.1
> Windows 10
>
> Anybody have a solution for this problem?
> Thanks a lot.
>
>
>
>
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org
> List info:https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-user
--
Johannes Kröger / GIS-Entwickler/-Berater
**********************************************
FOSSGIS Konferenz
15.-18. März 2023 in Berlin
https://fossgis-konferenz.de/2023/
WhereGroup-Beiträge auf der FOSSGIS
https://wheregroup.com/unternehmen/aktuelles/
**********************************************
WhereGroup GmbH
Grevenweg 89
20537 Hamburg
Germany
Tel: +49 (0)228 / 90 90 38 - 36
Fax: +49 (0)228 / 90 90 38 - 11
johannes.kroeger at wheregroup.com
www.wheregroup.com
Geschäftsführer:
Olaf Knopp, Peter Stamm
Amtsgericht Bonn, HRB 9885
-------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230215/4171803f/attachment-0001.htm>
More information about the QGIS-User
mailing list