3
qid = QInputDialog()
title = "Enter Coordinates"
label = "X:" 
mode = QLineEdit.Normal    
text, ok = QInputDialog.getText(qid, title, label, mode)
print text

I am trying to create a dialog box in which I can zoom into specific coordinates. However, I'm not sure on how to get both X and the Y coordinates. The picture below shows only X.

Current Dialog Box

0
2

Add a basic example using QInputDialog for make zoom to input coordinates

qid = QInputDialog()
canvas = iface.mapCanvas()
input, ok = QInputDialog.getText( qid, "Enter Coordinates", "Enter New Coordinates as 'xcoord,ycoord'", QLineEdit.Normal, "X" + "," + "Y")
if ok:
    x = input.split( "," )[ 0 ]
    print x
    y = input.split( "," )[ 1 ]
    print y
    if not x:
        print "Ooops!X value is missing!"
    if not y:
        print "Ooops!Y value is missing!"
    print x + "," + y
    scale=50
    rect = QgsRectangle(float(x)-scale,float(y)-scale,float(x)+scale,float(y)+scale)
    canvas.setExtent(rect)
    pt = QgsPoint(float(x),float(y))
    canvas.refresh()

Sample coord,for example is : -418795,4911670

9
  • Thanks for this. I am geting an error "Global name 'iface' is not defined. I then added self.iface = iface but it doesnt seem to like that either. :(
    – Sim
    Jan 12 '18 at 14:22
  • put this qgis.utils.iface
    – Fran Raga
    Jan 12 '18 at 14:31
  • No luck! NameError: global name 'qgis' is not defined
    – Sim
    Jan 12 '18 at 14:43
  • import this from qgis.utils import *
    – Fran Raga
    Jan 12 '18 at 14:45
  • It's still giving the same error!
    – Sim
    Jan 12 '18 at 14:48
1

QInputDialog is designed to get a single value. Is you want a more advanced input you should use a form.

You have an example on how to create a form in this stackoverflow answer (to a similar question).

1

QInputDialog() only gets a single value as explained in the documentation. One option is to change the label to "coordinates". The user could then enter both x and y in one line, separated by a comma. You can then get the individual values by using python.

x = text.split(",")[0]
y = text.split(",")[1] 

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.