5

I've generated a QGIS plugin using the QGIS Plugin Builder v3.0.3. One issue I am encountering with it is - as soon as I click the "OK" button (that invokes the run method) - the dialog closes. Is there anyway to prevent this behavior? The plugin I am developing is downloading data from a REST api - which can take some time depending on extent.

Ideally I'd like to keep to the dialog open - and showing an indeterminate progress bar - until the data is done downloading. What happens now is the dialog closes immediately, and then a few seconds later - the raster is added to the map.

Run method from the plugin below. I didnt alter anything else in the code templates generated by the Plugin Builder.

def run(self):
    # show the dialog
    self.dlg.show()

    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result:
        # build dem_url from input form parameters
        # what you see here is just a dummy url for this snippet
        dem_url = 'http://localhost/dem'
        r = requests.get(dem_url,stream=True)
        output_dem = self.dlg.lineEdit.text()
        with open(output_dem,'wb') as out:
             for chunk in r.iter_content(chunk_size=1024):
                 if chunk:  # filter out keep-alive new chunks
                     out.write(chunk)

        iface.addRasterLayer(output_dem, os.path.split(output_dem)[1])

1 Answer 1

0

To avoid a Dialog close when you click ok, you must disconnect your accepted button previously.

self.buttonBox.accepted.disconnect()

After disconnecting your button, you can connect to the button.

self.buttonBox.accepted.connect(self.run)

If you require to close your Dialog, you should add the following line:

def run(self):
    self.close()

Your Answer

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

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