3

I have a plugin that has a psycopg2 connection to a PostGIS database. When i close GUI with the X-Button and open it again, all the widgets are filled with the data from before. It looks like the X is only hiding the gui. Therefore I set the close event in the gui with

def closeEvent(self, event):
    qgis.utils.reloadPlugin('import_PostGis')

with the reloadPlugin methon I found in the QGIS utils file:

def reloadPlugin(packageName):
    """ unload and start again a plugin """

When the plugin is reseted it doesn't unload the Plugin from the tool bar but creates another icon from this plugin.

def initGui(self):
    """Create the menu entries and toolbar icons inside the QGIS GUI."""

    icon_path = ':/plugins/import_PostGIS/icon.png'
    self.add_action(
        icon_path,
        text=self.tr(u'PostGis Import'),
        callback=self.run,
        parent=self.iface.mainWindow())

def unload(self):
    """Removes the plugin menu item and icon from QGIS GUI."""
    for action in self.actions:
        self.iface.removePluginMenu(
            self.tr(u'&PostGis Import'),
            action)
        self.iface.removeToolBarIcon(action)

So every time I close the plugin a new plugin is generated in the toolbar. Is something in my code wrong or is there another possibility to clear the widgtes of the plugin before starting it again?

  • 1
    Rather than reloading the whole plugin, why don't you write a function that resets the UI? You don't reboot your computer every time you want to close a program. – Snorfalorpagus Oct 23 '14 at 12:13
2

Adding a toolbar:

# Create toolbar
self.toolbar = self.iface.addToolBar("My Toolbar")
self.toolbar.setObjectName("My toolbar Plugin")
self.toolbar.addAction(self.action)
self.btn = QAction(QIcon(":/plugins/import_PostGIS/icon.png"), "button1",  self.iface.mainWindow())
QObject.connect(self.btn, SIGNAL("triggered()"), self.method)
self.toolbar.addActions([self.btn])

and removing:

def unload(self):
    del self.toolbar
  • Unfortunately this doesn't work. There is still another toolbar entry created when I close the plugin. – Martin Oct 23 '14 at 8:46
  • Don't loop through the actions, remove them individually. – Matt Oct 23 '14 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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