1

I've got a Plugin widget box I'm trying to configure. In one step the user defines the path to where they will export the final layout as a .pdf file. I'm using the following code to configure the box:

class CreatePrintLayoutAndExportMap(QgsProcessingAlgorithm):
    OUTPUT_PDF_PATH = 'OUTPUT_PDF_PATH'
    OUTPUT_PDF_FILENAME_DEFAULT = 'Choose file path for pdf output'

    def initAlgorithm(self, config):

        self.addParameter(
            QgsProcessingParameterFileDestination(
                self.OUTPUT_PDF_PATH,
                self.tr(self.OUTPUT_PDF_FILENAME_DEFAULT),
                ".pdf"))

Here is what the box looks like as a GUI tool: enter image description here

This is good. The issue here is when a user specifies the destination, the file extension is not included and they must type "...filename.pdf" and the file extension is not manually included. Here is what it looks like in action: enter image description here

and then back in the widget box. Notice the .pdf file extension is not included automatically: enter image description here

If I run the tool, the file is saved as some weird file with no file type. If I explicitly include .pdf at the end of the filename (ex: "...filename.pdf"), I get a .pdf file

I've been looking at documentation for QgsProcessingParameterFileDestination as well as for QgsProcessingParameterRasterDestination as a comparison. The interesting thing is that if I export a raster file using the RasterDestination class, the .tif file extension is automatically included, removing confusion for the user.

Here is an example of a Raster output. I did not have to manually type '.tif' for it to be included: enter image description here

Is there a way to automatically include a file extension when using QgsProcessingParameterFileDestination class?

  • 1
    Perhaps a platform issue? On a Windows machine, the .pdf is automatically appended to the file path entered. – Joseph Mar 26 at 14:15
  • 1
    Thanks Joseph. I'll test on a PC – Erich Purpur Mar 26 at 14:17
  • 1
    @Joseph after testing, you are right. The file path is automatically included on a PC. This is annoying but I can work with it. – Erich Purpur Apr 1 at 13:26

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.