I have written a few QGIS plugins based on dockwidget (Spectre Viewer and Stripchart viewer). When opened, the plugin window is expected to stay open and react to interactions with opened layers. This works fine, but an annoyance is that when the plugin is installed and active, its window will open as soon as I start QGIS. I do not want them to appear until I open the plugin from the menu or toolbar. How can I do this?
The problem seems to be the opposite of QGIS plugins open automatically when starting QGIS, but I cannot see how the answer to this could help me (I do not have any startup.py in my plugins)
-
This would probably require editing the plugin's python code manually, is that something you would do? Updates would revert this.bugmenot123– bugmenot1232022年06月04日 20:14:28 +00:00Commented Jun 4, 2022 at 20:14
-
Do you mean that even if the dock widget was open when qgis is closed, you want it to automatically not be open the next time qgis is first started?Ben W– Ben W2022年06月05日 08:23:51 +00:00Commented Jun 5, 2022 at 8:23
-
@BenW, yes. It does not make sense to have the plugin open before a project is opened, and it feels more natural to open it if I need it than to close it if I do not need it.MortenSickel– MortenSickel2022年06月05日 19:13:05 +00:00Commented Jun 5, 2022 at 19:13
2 Answers 2
I made the following edits to your plugin's qgisSpectre.py
file in my local installation which seem to have fixed the problem. You can make the same changes in your local installation to test, then update your plugin.
Move the line which creates the dialog instance from the initGui()
method to the __init__()
method and add the following lines like so:
self.dlg=qgisSpectreDockWidget(self.iface.mainWindow())
if self.dlg.isVisible():
self.dlg.close()
Also in the __init__()
method, change:
self.pluginIsActive = False
to:
self.pluginIsActive = None
Then set the self.pluginIsActive
attribute to False in the initGui()
method.
Now (November 2023, QGIS v 3.32) I noticed a similiar problem. A kind of "ghosty" dialog will show up after installation. That is the dialog without a background. Ben's solution did not help, but I noticed that after I had closed the widget once, it did not turn up again, so I just unconditionally close the widget in the init, so now the start of the init is
self.iface = iface
self.dlg=qgisSpectreDockWidget(self.iface.mainWindow())
# stops it from showing up when starting QGIS
#if self.dlg.isVisible():
try:
self.dlg.close()
except:
print("Should not be open here")
# It should not open when QGIS is started
self.pluginIsActive = None
I wrapped the close() in a try except in case it fails out for some reason. As it probably already is closed, I just catch the exception without any further handling.