1

I used the QGIS Plugin Builder to create a plugin where the main.py file imports dockwidgets and class from its own file and from other files. A button on the toolbar executes the run(self) function in main.py where it loads the imported dockwidgets, classes, connecting buttons to functions etc.

Another button on the toolbar closes the plugin by executing the onClosePlugin(self) function. This function only closes dockwidgets loaded from main.py (so not from other files) and disconnects its buttons. The imported classes are untouched. However, when I close the plugin, QGIS continues to run but a lot slower than usual. It also takes longer to close down, sometimes I get an error report. I'm guessing this is because of all the resources my plugin loads but is still in memory when the plugin is closed.

How can I remove/unload all resources that have been imported/loaded by the plugin?

When I import dockwidgets and classes from other files, I use the following in my main.py

from some_Dockwidget import someDockwidget
from general_Class import generalClass

Which are then called in run(self):

self.someDockwidget = someDockwidget()
self.generalClass = generalClass()

I found this post which closes all QDialog boxes. I have used the following to close all dockwidgets but can it be adapted to close/unload/delete everything that has been imported/loaded?

items = vars(self)
for i in items:
    item = items[i]
    if isinstance(item, QDockWidget):
        item.close()
3
  • 1
    I feel that the question is a bit opinion based, and finding the correct answer will be hard, but if you pointed out a bit better, what exactly your requirements are, this should be perfectly answerable and valueable =)
    – Erik
    Commented May 18, 2022 at 14:45
  • @Erik thanks, I'll edit my question :D
    – user35594
    Commented May 18, 2022 at 14:48
  • Garbage collection of Qt widgets is even less obvious than normal Python garbage collection. You may need to use the deleteLater method of Qt objects - geeksforgeeks.org/deletelater-method-in-pyqt5. You might have better luck asking this on a more programming oriented forum, like StackOverflow
    – Llaves
    Commented May 18, 2022 at 16:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.