2

I've written a small python plugin (using plugin builder as base) to simplify the expression process for a given layer (just a few comboboxes that are used to create a query). The combo box populating is done in the def run(self): part with a simple .addItem loop. However when I close the plugin and reopen it again, the values are added again. So if it's "a, b, c" and I close/open the plugin again, it's "a, b, c, a, b, c" in the box.. can I stop/avoid that? Or should I just wrap it in a case that sets a flag on the first run?

  • 1
    From your comment, if the data you show in the ComboBox is more or less static, you could check in your run method, whether the ComboBox has already items and avoid duplicate them. If, on the contrary, the ComboBox is empty you could proceed and add items. Does it make sense for you? It really depends on the use case. – Germán Carrillo Feb 28 '15 at 20:16
  • Yes - pretty much the same idea as setting a flag. Since it depends on the use case I figured I'll add an option for this and/or a manual refresh button. – MapEngine Mar 1 '15 at 10:21
1

In my opinion you should empty the combo box before populating it.

The rationale behind that is that the layer might have changed so it's not a good idea to set a flag and ignore everything that might happen afterwards.

  • 1
    Clearly the preferrable solution but if there's a tons of data, it'll be slow (potentially for no reason). But that's dependent on the layer or user I guess. If anyone wonders how it's done, self.dlg.<comboboxname>.clear() – MapEngine Feb 28 '15 at 19:51
1

Both underdark♦'s and gcarrillo's replies provide valid answers. How exactly the issue should be handled however depends on the use case, and if necessary, options should be provided.

1

You can overwrite the close function of your UI as well, that the plugin is reloaded every time you close it.

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

This will reload the plugin completely and will empty all your boxes. In this way you don't have to care about clearing the boxes because the plugin will always load only with the values you have defined. I use this function also for testing, because if you write new code and close your plugin it will reload the newest code as well.

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.