3

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])

0

Your Answer

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

Browse other questions tagged or ask your own question.