<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Load and Unload an instance of Qt Form]]></title><description><![CDATA[<p dir="auto">Is this possible in PyQt? - I would like to <strong>load</strong> on demand (called by a string reference) a Qt form with PyQt code into a main form, retaining a separate namespace for the instance of the form and cleanly <strong>unload</strong> the form when closed by user.</p>
<p dir="auto">Principally the same thing how a Form in winform solution works. Each form there is stored in separate file(s) (code, form definition and designer file - in PyQt it would be a single file), can be loaded with <code>Dim frm as New MyForm</code> and <code>frm.Show()</code> and closed with <code>Me.Close()</code>. Within the instance of the form a local namespace works, ensuring the main application doesn't get cluttered and instances mixed. If needed, one can drill into parent's namespace.</p>
<p dir="auto">I found close to nothing about the subject during my very extensive research. So either I'm so green in Python and it's so obvious that no one needs to ask, or it's rare to ask this from a PyQt application or it's too much to ask from Python by it's definition. Please don't stone me, if it's an apparent thing.</p>
<p dir="auto">I did some tests myself loading the forms as standard python modules, but got into issues with main window name space cluttering, broken signals when more instances of one form existed and I don't think I really got the garbage collector to clean up stuff when the UI part of the module was <code>.close()</code>d. I'm not including any code for wouldn't provide much lead, if any.</p>
]]></description><link>https://forum.qt.io/topic/118833/load-and-unload-an-instance-of-qt-form</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 04:29:48 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/118833.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 08 Sep 2020 16:28:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Wed, 09 Sep 2020 11:49:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/oak77">@<bdi>Oak77</bdi></a> said in <a href="/post/616470">Load and Unload an instance of Qt Form</a>:</p>
<blockquote>
<p dir="auto">What I meant is, that the XML doesn't bear any python code, just UI definitions. But perhaps I could import a module which in turn loads the UI file directly, to keep it clean.</p>
</blockquote>
<p dir="auto">That is what <code>pyuic5</code>, the way I'm recommending, does!  You run <code>pyuic5</code> on your <code>.ui</code> file (during developemnt) and it creates a <code>.py</code> file of Python code implementing all of the UI in a Python class, ready for you to use via <code>import</code>, as per e.g. the first example on that linked page.  That is the approach most people take from C++.</p>
]]></description><link>https://forum.qt.io/post/616474</link><guid isPermaLink="true">https://forum.qt.io/post/616474</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 09 Sep 2020 11:49:55 GMT</pubDate></item><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Wed, 09 Sep 2020 11:24:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">I did mean the <em>instance</em>, but I didn't realize all the details and consequences. I think you're right and put that way, it really makes sense.</p>
<blockquote>
<p dir="auto">...<code>pyuic5</code>-generate-class vs <code>loadUI()</code> are alternatives.</p>
</blockquote>
<p dir="auto">Oh, I see, I mixed the two together. I meant actually only loading/importing the resulting python UI class.</p>
<blockquote>
<p dir="auto">What's wrong with, say, the very first example approach at <a href="https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html</a> ?</p>
</blockquote>
<p dir="auto">What I meant is, that the XML doesn't bear any python code, just UI definitions. But perhaps I could import a module which in turn loads the UI file directly, to keep it clean.</p>
<p dir="auto">I apologize for my confused questions and thank you very much for helping me. You explained it very well. I'll read the relevant documentation again and test all sugested scenarios.</p>
]]></description><link>https://forum.qt.io/post/616470</link><guid isPermaLink="true">https://forum.qt.io/post/616470</guid><dc:creator><![CDATA[Oak77]]></dc:creator><pubDate>Wed, 09 Sep 2020 11:24:30 GMT</pubDate></item><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Wed, 09 Sep 2020 10:16:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/oak77">@<bdi>Oak77</bdi></a> said in <a href="/post/616454">Load and Unload an instance of Qt Form</a>:</p>
<blockquote>
<p dir="auto">VB will cleanly dispose the loaded module.</p>
</blockquote>
<p dir="auto">But it doesn't "dispose the loaded module", at least not the VB code you show.  It disposes the <em>instance</em> you create (<code>Dim frm as New MyForm</code>), not the code/module itself.  Python will release the instance when you no longer retain any references to it, as with all things Python.</p>
<p dir="auto">I still don't see why on the one hand you say "Generate python UI class", i.e. using <code>pyuic5</code>, and then still you are using <code>pyuic5.loadUI()</code>.  I refer you again to <a href="https://stackoverflow.com/questions/52471705/why-in-pyqt5-should-i-use-pyuic5-and-not-uic-loaduimy-ui" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/52471705/why-in-pyqt5-should-i-use-pyuic5-and-not-uic-loaduimy-ui</a>.  <code>pyuic5</code>-generate-class vs <code>loadUI()</code> are <em>alternatives</em>.  What's wrong with, say, the very first example approach at <a href="https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html</a> ?</p>
<p dir="auto">At this point I suggest you create some <em>minimal</em> code for what you want to do, and see how that goes.</p>
]]></description><link>https://forum.qt.io/post/616458</link><guid isPermaLink="true">https://forum.qt.io/post/616458</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 09 Sep 2020 10:16:27 GMT</pubDate></item><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Wed, 09 Sep 2020 09:55:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I could have been wrong in understanding how the things work in PyQt. I know as a fact that the described process in VB will cleanly dispose the loaded module. Users easily open and close hundreds of modules per working day and the RAM usage never spirals up. It would be of course beautiful if <code>close()</code> would do the same.</p>
<p dir="auto">I'll have to study pyuic5 and loading, no doubt. I'll start with roughly this scenario:</p>
<ol>
<li>Define sample UI file for a module</li>
<li>Generate python UI class</li>
<li>Add working code into that python UI class, such as loading data from database upon selection and button click or drawing into canvas, etc.</li>
<li>Define <code>self.dw1 = QDockWidget</code></li>
<li><code>pyuic5.loadUI(myUIclass.py, self.dw1)</code> - I'm not sure if the syntaxe  is right, but I will try to load the UI into the <code>QDockWidget</code></li>
<li>Test if code in module is working</li>
<li>Close the module (i.e. by close button of the <code>QDockWidget</code> and see if RAM doesn't bloat, when the module(s) are opened and closed many times and/or use debugger to monitor the status</li>
</ol>
<p dir="auto">I'd be pleased if you could confirm if those steps sound roughly correct.</p>
]]></description><link>https://forum.qt.io/post/616454</link><guid isPermaLink="true">https://forum.qt.io/post/616454</guid><dc:creator><![CDATA[Oak77]]></dc:creator><pubDate>Wed, 09 Sep 2020 09:55:50 GMT</pubDate></item><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Wed, 09 Sep 2020 08:11:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/oak77">@<bdi>Oak77</bdi></a></p>
<blockquote>
<p dir="auto">and I don't think it's possible/meant to be bundled with working code</p>
</blockquote>
<p dir="auto">No idea what you mean here.  The <code>loadUI()</code> route is perfectly acceptable for "working" code, if you want to supply the <code>.ui</code> file at run-time and load it dynamically.</p>
<p dir="auto">With <code>pyuic5</code> you do not supply the <code>.ui</code> file at run-time, but instead you have generated a <code>.py</code> code file off it and you supply that instead.</p>
<p dir="auto">There isn't any "unloading", just closing.  But there isn't any "unloading" in that sense in the VB code you show either.  You asked for</p>
<blockquote>
<p dir="auto">Dim frm as New MyForm and frm.Show() and closed with Me.Close()</p>
</blockquote>
<p dir="auto">and that's what either way would do, with the <code>pyuic5</code> route being closer as it allows the new class as in <code>Dim frm as New MyForm</code>.</p>
<p dir="auto">There are things you can do in Python not available in C++ to "remove" code/classes at runtime.  But I think you are getting hung up on this.  Why do you keep talking about "how do I unload the module?" when there is no need to try to do this?  Just create your widgets, show &amp; close them.  Let your Python references to forms go our of scope, or <code>Nothing</code> them, and maybe consider using <code>Qt::WA_DeleteOnClose</code> (<a href="https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qt.html#WidgetAttribute-enum</a>) on, say, your forms/dialogs to ensure they get deleted at the Qt side.</p>
]]></description><link>https://forum.qt.io/post/616415</link><guid isPermaLink="true">https://forum.qt.io/post/616415</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 09 Sep 2020 08:11:00 GMT</pubDate></item><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Wed, 09 Sep 2020 07:58:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a>  I looked into the two utilities and realized I've used both of them in course of my learning and getting familiar with PyQt. In the 2nd case, <code>uic.loadUi()</code> loads an XML definition of the form made by QtDesigner and I don't think it's possible/meant to be bundled with working code.</p>
<p dir="auto">In the first case, <code>pyuic5</code> is interesting. It converts XML into a python class with Qt definitions. In theory, it would load any proper python UI class, regardless if it's made by user or QtDesigner.<br />
So I could imagine a way to make the module this way. But I am lost as for:</p>
<blockquote>
<p dir="auto">The resulting instance can be shown &amp; closed in the same way as the VB.</p>
</blockquote>
<p dir="auto">I'm sorry, perhaps I ask for something very obvious, but could you please point me at the right direction - <em>how</em> do I unload the module? I couldn't find anything about it <code>pyuic5</code> and unloading.</p>
]]></description><link>https://forum.qt.io/post/616414</link><guid isPermaLink="true">https://forum.qt.io/post/616414</guid><dc:creator><![CDATA[Oak77]]></dc:creator><pubDate>Wed, 09 Sep 2020 07:58:09 GMT</pubDate></item><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Tue, 08 Sep 2020 19:15:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thank you very much, it is well possible, that you directing me to this post is all I needed. I somehow missed this particular post. I've seen some examples (very few) of loading via uic, but it seemed to me it's purpose different. I will study both in detail.<br />
Once gain, thank you very much!</p>
]]></description><link>https://forum.qt.io/post/616349</link><guid isPermaLink="true">https://forum.qt.io/post/616349</guid><dc:creator><![CDATA[Oak77]]></dc:creator><pubDate>Tue, 08 Sep 2020 19:15:13 GMT</pubDate></item><item><title><![CDATA[Reply to Load and Unload an instance of Qt Form on Tue, 08 Sep 2020 17:14:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/oak77">@<bdi>Oak77</bdi></a><br />
To start, have you read <a href="https://stackoverflow.com/questions/52471705/why-in-pyqt5-should-i-use-pyuic5-and-not-uic-loaduimy-ui" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/52471705/why-in-pyqt5-should-i-use-pyuic5-and-not-uic-loaduimy-ui</a> and the solution there with its references?</p>
<p dir="auto">You can <code>uic.loadUi()</code> if you want to go down the dynamic route.  I prefer the the <code>pyuic5</code> route of getting a class.  The resulting instance can be shown &amp; closed in the same way as the VB.</p>
<p dir="auto">I don't quite what you mean about namespace etc. stuff.  There is no clashing of variable/class names.</p>
<p dir="auto">Unless there is something unsatisfactory with these routes I don't see what further you would want to achieve.</p>
]]></description><link>https://forum.qt.io/post/616328</link><guid isPermaLink="true">https://forum.qt.io/post/616328</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 08 Sep 2020 17:14:56 GMT</pubDate></item></channel></rss>