<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi Manfred,</p>
<p>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?<br>
</p>
<p>Cheers, Hannes<br>
</p>
<div class="moz-cite-prefix">Am 14.02.23 um 22:11 schrieb Manfred
Strahlhofer via QGIS-User:<br>
</div>
<blockquote type="cite"
cite="mid:CAOvCW0W=DvQ-CnVrU1Xh6cq8S_qchw3yPDv9i8szmKDYZbV6YQ@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small">Hello!</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><br>
</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small">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:</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small">
<div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre"><div><span style="color:rgb(106,153,85)"># sample code below draws a canvas with a plot</span></div><div><span style="color:rgb(106,153,85)"># however closing this plot canvas crashes the plugin (cannot be restarted)</span></div><div><span style="color:rgb(106,153,85)"># and further qgis can't be closed with the X button</span></div><div><span style="color:rgb(106,153,85)"># originates from: <a href="https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py" moz-do-not-send="true" class="moz-txt-link-freetext">https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py</a></span></div><div><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">sys</span></div><div><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">time</span></div>
<div><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">numpy</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">as</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">np</span></div>
<div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">matplotlib</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">backends</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">qt_compat</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> QtWidgets</span></div><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">matplotlib</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">backends</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">backend_qtagg</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> (</span></div><div><span style="color:rgb(212,212,212)"> FigureCanvas, NavigationToolbar2QT </span><span style="color:rgb(197,134,192)">as</span><span style="color:rgb(212,212,212)"> NavigationToolbar)</span></div><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">matplotlib</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">figure</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">Figure</span></div>
<div><span style="color:rgb(86,156,214)">class</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">ApplicationWindow</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(78,201,176)">QtWidgets</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">QMainWindow</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">__init__</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">super</span><span style="color:rgb(212,212,212)">().</span><span style="color:rgb(220,220,170)">__init__</span><span style="color:rgb(212,212,212)">()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_main</span><span style="color:rgb(212,212,212)"> = QtWidgets.QWidget()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.setCentralWidget(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_main</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)"> = QtWidgets.QVBoxLayout(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_main</span><span style="color:rgb(212,212,212)">)</span></div>
<div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)"> = FigureCanvas(</span><span style="color:rgb(78,201,176)">Figure</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">figsize</span><span style="color:rgb(212,212,212)">=(</span><span style="color:rgb(181,206,168)">5</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">3</span><span style="color:rgb(212,212,212)">)))</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)"># Ideally one would use self.addToolBar here, but it is slightly</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)"># incompatible between PyQt6 and other bindings, so we just add the</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)"># toolbar as a plain widget instead.</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(NavigationToolbar(</span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">))</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(</span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)">)</span></div>
<div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)"> = FigureCanvas(</span><span style="color:rgb(78,201,176)">Figure</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">figsize</span><span style="color:rgb(212,212,212)">=(</span><span style="color:rgb(181,206,168)">5</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">3</span><span style="color:rgb(212,212,212)">)))</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(</span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(NavigationToolbar(</span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">))</span></div>
<div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_static_ax</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)">.figure.subplots()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">linspace</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">10</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">501</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_static_ax</span><span style="color:rgb(212,212,212)">.plot(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">tan</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">), </span><span style="color:rgb(206,145,120)">"."</span><span style="color:rgb(212,212,212)">)</span></div>
<div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_dynamic_ax</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">.figure.subplots()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">linspace</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">10</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">101</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)"># Set up a Line2D.</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_line</span><span style="color:rgb(212,212,212)">, = </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_dynamic_ax</span><span style="color:rgb(212,212,212)">.plot(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">sin</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> + </span><span style="color:rgb(78,201,176)">time</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">time</span><span style="color:rgb(212,212,212)">()))</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_timer</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">.new_timer(</span><span style="color:rgb(181,206,168)">50</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_timer</span><span style="color:rgb(212,212,212)">.add_callback(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">_update_canvas</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_timer</span><span style="color:rgb(212,212,212)">.start()</span></div>
<div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">_update_canvas</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">linspace</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">10</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">101</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)"># Shift the sinusoid as a function of time.</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_line</span><span style="color:rgb(212,212,212)">.set_data(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">sin</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> + </span><span style="color:rgb(78,201,176)">time</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">time</span><span style="color:rgb(212,212,212)">()))</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_line</span><span style="color:rgb(212,212,212)">.figure.canvas.draw()</span></div>
<div><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">test_plot</span><span style="color:rgb(212,212,212)">():</span></div>
<div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)"># Check whether there is already a running QApplication (e.g., if running</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)"># from an IDE).</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)">#qapp = QtWidgets.QApplication.instance() this stucks QGIS!</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)">#if not qapp:</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">qapp</span><span style="color:rgb(212,212,212)"> = QtWidgets.QApplication(</span><span style="color:rgb(78,201,176)">sys</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">argv</span><span style="color:rgb(212,212,212)">) </span><span style="color:rgb(106,153,85)"># this stucks QGIS main window when the dialog's close button is pressed</span></div>
<div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">ApplicationWindow</span><span style="color:rgb(212,212,212)">()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)">.show()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)">.activateWindow()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)">.raise_()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">qapp</span><span style="color:rgb(212,212,212)">.exec()</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(106,153,85)">#app.exit()</span></div>
</div>
</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><br>
</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small">When
using "<span style="color:rgb(0,0,0)">qapp =
QtWidgets.QApplication.instance()</span>", QGIS stucks
completely and no plot is shown ever.</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small">When
using <span style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)">"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.</span></span></div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)">I am using:</span></span></div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)">QGIS Version
3.24.1 Tisler</span></span></div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)">matplotlib 3.5.1<br>
</span></span></div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)">Windows 10<br>
</span></span></div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)"><br>
</span></span></div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)">Anybody have a
solution for this problem?</span></span></div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="color:rgb(0,0,0)"><span
style="background-color:rgb(255,255,255)">Thanks a lot.<br>
</span></span>
</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><br>
</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><span
style="background-color:rgb(255,255,0)"><span style=""></span></span><br>
</div>
<div class="gmail_default"
style="font-family:verdana,sans-serif;font-size:small"><br>
</div>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
QGIS-User mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-User@lists.osgeo.org">QGIS-User@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
</blockquote>
<pre class="moz-signature" cols="72">--
Johannes Kröger / GIS-Entwickler/-Berater
**********************************************
FOSSGIS Konferenz
15.-18. März 2023 in Berlin
<a class="moz-txt-link-freetext" href="https://fossgis-konferenz.de/2023/">https://fossgis-konferenz.de/2023/</a>
WhereGroup-Beiträge auf der FOSSGIS
<a class="moz-txt-link-freetext" href="https://wheregroup.com/unternehmen/aktuelles/">https://wheregroup.com/unternehmen/aktuelles/</a>
**********************************************
WhereGroup GmbH
Grevenweg 89
20537 Hamburg
Germany
Tel: +49 (0)228 / 90 90 38 - 36
Fax: +49 (0)228 / 90 90 38 - 11
<a class="moz-txt-link-abbreviated" href="mailto:johannes.kroeger@wheregroup.com">johannes.kroeger@wheregroup.com</a>
<a class="moz-txt-link-abbreviated" href="http://www.wheregroup.com">www.wheregroup.com</a>
Geschäftsführer:
Olaf Knopp, Peter Stamm
Amtsgericht Bonn, HRB 9885
-------------------------------
</pre>
</body>
</html>