2

I have a plugins generated by "plugins builder" with simple Ok / Cancel button / combo box to display canvas layers. When i cick on "OK" button, plugins is not close and if i use it again, my components are not reloaded as first time and add again existing values.

enter image description here

I could insert condition in my python code to not load layer again if already exist in my combo box but i think it's better to entirely close and open plugin without taking into account the previous state.

Same result is know when user click on window's red cross to close.

What is the function to properly close plugins ?

  • Could you please edit your question to include the main part of the code you used? At the start of your function which inserts the items into your comboBox, you could first clear it using self.comboBox.clear(). If you want to close the dialog, you could use self.dlg.close(). – Joseph Dec 4 '17 at 10:43
  • This question is about to be closed because is too broad. Your main issue is avoiding loading layers again if already exist in your combo box, however, you think for solving that it's better to entirely close and open plugin without taking into account the previous state. You shouldn't do that. – xunilk Dec 4 '17 at 15:52
1

This is the right way to avoid loading layers again if already exist in your combo box. So, it is not better to entirely close and open plugin without taking into account the previous state. You shouldn't do that.

   .
   .
   .
    def __init__(self, iface):
   .
   .
   .
        self.dlg.comboBox.clear()
        layers = self.iface.legendInterface().layers()
        layer_list = [ layer.name() for layer in layers ]
        self.dlg.comboBox.addItems(layer_list)
   .
   .
   .
    def change_layers(self):
        self.dlg.comboBox.clear()
        layers = self.iface.legendInterface().layers()
        layer_list = [ layer.name() for layer in layers ]
        self.dlg.comboBox.addItems(layer_list)

    def run(self):
        """Run method that performs all the real work"""
        self.iface.legendInterface().groupRelationsChanged.connect(self.change_layers)
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with you
  • Indeed, i think this solution is the right way. it's not my final solution but this concept work for me with self.dlg.comboBox.clear(). – Gaet Dec 5 '17 at 8:32
  • Good for you. Remember that is one topic for question until your final solution. Could you please edit your question for adapting to this solution? Thanks. – xunilk Dec 5 '17 at 11:53
1

Try adding this function :

def closeEvent(self, event):
        self.closingPlugin.emit()
        event.accept()

And call it when you want to close your dialog

  • I have an error. Because plugin builder create run() function trigger when user click on "Ok" button. I could use pushButton to realize that. – Gaet Dec 4 '17 at 10:12
  • if user click on red cross to close windows => same result and no way to force re initialization. – Gaet Dec 4 '17 at 10:12
0

If you want to completely reload your plugin you can use reloadPlugin function from qgis.utils:

from qgis.utils import reloadPlugin

...

def reload_plugin(self):
    reloadPlugin('plugin_name')

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