Wednesday, 5 April 2017

python - PyQGIS reload a QGIS plugin


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?



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...