9

I want to make a drop down box that shows 2 or 3 options and let the user choose 1 from them?

I would like to let the user to choose from "millimeter", "centimeter" and "meter". These Options are not from the field of input.

It seems like none of them here is valid:

It seems like none of them here is valid

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 3, 2016 at 10:12

3 Answers 3

9

If you are not limited to QGIS v2.8 (your screenshot points to v2.8 documentation), you can install a newer version. For example, in QGIS v2.14 you have a selection parameter type available (see the docs):

enter image description here

You can use it in this way in the header of the script, separating options with a semicolon:

##Units=selection Millimeter;Centimeter;Meter

And later in the script you can get the chosen value in this way:

if Units == 0:
 # User chose millimeter
elif Units == 1:
 # User chose centimeter
elif Units == 2:
 # User chose meter

For reference, here you have an example script.

answered Dec 3, 2016 at 13:39
1
  • Thanks a lot but I am limited to QGIS v2.8, is there a way to do it in v2.8? Commented Dec 4, 2016 at 5:12
5

Germán Carrillo's answer is the most convenient. A possible alternative (if you're stuck with QGIS 2.8) could be to:

  • Use a string which allows the user to enter the units they want to use:

    ##units=string mm
    if units == 'mm':
     # Do something
    elif units == 'cm':
     # Do something
    elif units == 'm':
     # Do something
    

    Text units


  • Or use three boolean checkboxes:

    ##Millimeter=boolean
    ##Centimeter=boolean
    ##Meter=boolean
    if Millimeter == True:
     # Do something
    elif Centimeter == True:
     # Do something
    elif Meter == True:
     # Do something
    

    Checkbox units

answered Dec 5, 2016 at 12:02
0

The selection option does work on QGIS 2.18.2 (Las Palmas). Please note that the selection parameter will get an integer value,starting from 0, (and not a string value) correspond to the order of the string as you write it in your selection (just like in Germán Carrillo answer).

answered Jan 21, 2017 at 8:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.