2

I'm adding some stuff to a QGIS plugin and trying to figure out how to work with user inputs. In this case, the user input is a string, which seems simple to work with, but I can't find an example of it anywhere.

I am not using Qt Designer to design my interface for the dialog box, I'm instead using various methods from the QgsProcessingParameters class. Specifically I am using QgsProcessingParameterString.

In the plugin, I'm using the initAlgorithm and adding a line for the user to input a string. Here is the code:

def initAlgorithm(self, config):

#Add String as input
    self.addParameter(
        QgsProcessingParameterString(
            self.INPUT_TITLE,
            self.tr('Input title string (Optional)')
        )
    )

Now in the processAlgorithm I use a method .parameterAsString

def processAlgorithm(self, parameters, context, feedback):
    """This actually does the processing for creating the print layout and exporting as .pdf"""

    log = feedback.setProgressText

    input_title = self.parameterAsString(parameters, self.INPUT_TITLE, context)

    log(print the user input)

I'm just trying to print the user input in this example. How do I get the value the user input (a string)? I want to create a variable which stores the user input as a string. I can use the string later in my program.

And are there any examples of using QgsProcessingAlgorithm.parameterAsString?

  • 1
    What you've got is perfect! – ndawson Feb 14 at 3:51
2

@ndawson's simple comment gave me the confidence I was actually on the right track

I will elaborate on my own question. In the processAlgorithm, the log prints stuff to the console when running the plugin. I define input_title as the following:

def processAlgorithm(self, parameters, context, feedback):

    log = feedback.setProgressText

    input_title = self.parameterAsString(parameters, self.INPUT_TITLE, context)

input_title is now just a string, with the text the user wrote in the dialog box of the plugin. I was definitely overthinking this earlier.

While running the plugin, if I want to return the user's input text in the "log" tab of my plugin, I can do:

def processAlgorithm(self, parameters, context, feedback):

    log = feedback.setProgressText

    input_title = self.parameterAsString(parameters, self.INPUT_TITLE, context)

    log(input_title)      #this prints input_title in plugin log

Alternatively, I can work with vector or raster objects in the same way. Here is an example with a vector object: First, create your class and create constants to refer to parameters. In the initAlgorithm, define an vector input field. Then in the processAlgorithm you can work with the vector object.

class ClassName(QgsProcessingAlgorithm):
    INPUT_VECTOR = 'INPUT_VECTOR'

    def initAlgorithm(self, config):
    """Here we define the inputs and output of the algorithm"""

        #Add Vector Layer as input
        self.addParameter(
            QgsProcessingParameterVectorLayer(
                self.INPUT_VECTOR,
                self.tr('Choose vector layer as input'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )

    def processAlgorithm(self, parameters, context, feedback):

        log = feedback.setProgressText

        input_vector = self.parameterAsVectorLayer(parameters, self.INPUT_VECTOR, context)

        log(input_vector.name())        #use all the regular attributes and methods for a vector object

I hope this helps someone other than me.

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.