3

I created a plugin using QGIS Plugin Builder Plugin using the Template Tool Button with dock widget. Works fine, but I have to click the associated button each time I start QGIS in order to open the dock widget.

Question: How can I make QGIS open the dock widget at each start, or at least remind its state like those of the core plugins (GPS Information Panel for example)?

9
  • If I understand you correctly, you want your plugin to open up on start?: if so: just put the self.run() function into the __init__ of your plugin. However be aware, that functionality will work but QGIS won't open its own GUI till you close your plugin
    – LaughU
    Jun 21, 2017 at 8:12
  • Interesting, but not exactly what I want, since the plugin strongly interacts with the map map view in QGIS I need to open the plugin as a dock widget WITHIN QGIS when it starts. Jun 21, 2017 at 8:31
  • is the tool in development? if so you could start QGIS with plugin deactivated and when activating it from plugins -> manage and install plugins it will show up instantly.
    – LaughU
    Jun 21, 2017 at 8:33
  • @JochenSchwarze - Could you please clarify what you mean when you say "or at least remind its state like those of the core plugins"? For one of my projects, I created a macro which loads a specific plugin, stores the visible states of all toolbars and panels excluding the plugin icons and hides them, and when the project is closed, the toolbars and panels which were previously visible will be made visible again. You could turn this into a script that could be used at startup. Are you trying to do something similar?
    – Joseph
    Jun 21, 2017 at 8:59
  • 1
    Yep, for example when I open the above referred GPS Info Panel, dock it to the right, then close and reopen QGIS the GPS Info Panel is still where I put it before. I've been after this behaviour for my own plugins, but what you suggest sounds much better (again...). I have a QGIS running on a relativley small display and have to minimize it by means of making all what is not needed invisible, except my plugin. It's all about an application referred to here: goo.gl/cDAFfG ... and as you can see at first sight, the plugin should open at startup, and all the other things are obsolete. Jun 21, 2017 at 9:26

1 Answer 1

2
  1. In your main_plugin.py where you defined your buttons (usually in the initGui() function), make sure you assign it an objectName. For example:

    def initGui(self):
        self.pluginButton = self.add_action(
            ':/plugins/myPlugin/icon.png',
            text=self.tr(u'Start'),
            callback=self.run,
            parent=self.iface.mainWindow())
        self.pluginButton.setObjectName('Run_plugin')
    

    Then in the function which is associated with the button (in the example, run(self)), use some logic to hide everything excluding your plugin toolbar and dockwidget:

    def run(self):
        # Set toolbars visibility to False
        for x in iface.mainWindow().findChildren(QToolBar): 
            if x.objectName() == 'Run_plugin':
                x.setVisible(True)
            else:
                x.setVisible(False)
        # Set panels visibility to False
        for x in iface.mainWindow().findChildren(QDockWidget): 
            x.setVisible(False)
    
        # Define your dockwidget from its class
        self.dockwidget = PluginDockWidget()
        # Make your dockwidget visible
        self.dockwidget.show()
    

  1. Create a startup.py script and use something like the following:

    from qgis.utils import iface
    from PyQt4.QtGui import QAction
    
    def load():
        iface.mainWindow().findChild(QAction, 'Run_plugin').trigger()
    
    iface.initializationCompleted.connect(load)
    

    Once QGIS has fully loaded, it will trigger the button from your plugin toolbar which should then hide everything excluding your plugin toolbar and dockwidget.

1
  • Interesting, have the same question: just open automatically at startup a dockwidget plugin. I'm a newbie, and I do not find main-plugin.py, nor do I know where (in what directory) to store startup.py. Please clarify.
    – SWP_IB
    May 12, 2020 at 8:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.