1

I am trying to create a postcode search using a dialog box but if I press "cancel", it automatically shows ALL of the addresses within the area and doesn't just close the box. Not sure why!

def pc_search(self):  
#Get the postcode to search for
qid = QInputDialog()
title = "Enter Postcode"
label = "Postcode:"
mode = QLineEdit.Normal
default = ""

pc, ok = QInputDialog.getText(qid, title, label, mode, default)
print pc

# Open the address data table and search for the postcode
uri = QgsDataSourceURI()
uri.setConnection("ictdsgdev5", "5432", "gistest", "postgres", "gistest1")
uri.setDataSource("public", "OSADDBASE_LEICS", "geom", "POSTCODE like '" + pc.upper() + "%" + "'")
print uri.sql()
vlayer = QgsVectorLayer(uri.uri(), "Address", "postgres")
print vlayer.isValid()

if vlayer.isValid():
    # Add the address layer to the list of layers
    QgsMapLayerRegistry.instance().addMapLayer(vlayer)

    # Select the found addresses and zoom to them
    selectList=[]

    for feature in vlayer.getFeatures():
        geom = feature.geometry()
        selectList.append(feature.id())

    vlayer.setSelectedFeatures(selectList)

else:
    #MessageBox displaying no address found 
    QMessageBox.information(None, "Information", "No address(es) found for " + pc.upper())
1

You display the dialog box and get back the entered values and a boolean indicating if OK was pressed or not

pc, ok = QInputDialog.getText(qid, title, label, mode, default)

Then you you get the entered value and use it in a like query

uri.setDataSource("public", "OSADDBASE_LEICS", "geom", "POSTCODE like '" + pc.upper() + "%" + "'")

so when Cancelled is pressed, ok is false and pc is empty, resulting in the query POSTCODE like '%', which returns every record.

So, to overcome this, you can apply the query/whole code only if ok was pressed. Similarly, if you don't want to search for all postal code if someone leaves a blank string, you can add the and pc in the condition (as a string evaluates to false if it is empty)

def pc_search(self):  
#Get the postcode to search for
qid = QInputDialog()
title = "Enter Postcode"
label = "Postcode:"
mode = QLineEdit.Normal
default = ""

pc, ok = QInputDialog.getText(qid, title, label, mode, default)
print pc

if ok and pc: 

    # Open the address data table and search for the postcode
    uri = QgsDataSourceURI()
    uri.setConnection("ictdsgdev5", "5432", "gistest", "postgres", "gistest1")
    uri.setDataSource("public", "OSADDBASE_LEICS", "geom", "POSTCODE like '" + pc.upper() + "%" + "'")
    print uri.sql()
    vlayer = QgsVectorLayer(uri.uri(), "Address", "postgres")
    print vlayer.isValid()

    if vlayer.isValid():
        # Add the address layer to the list of layers
        QgsMapLayerRegistry.instance().addMapLayer(vlayer)

        # Select the found addresses and zoom to them
        selectList=[]

        for feature in vlayer.getFeatures():
            geom = feature.geometry()
            selectList.append(feature.id())

        vlayer.setSelectedFeatures(selectList)

    else:
        #MessageBox displaying no address found 
        QMessageBox.information(None, "Information", "No address(es) found for " + pc.upper())
  • Thanks. So I added the "if ok:" and yet it still shows all addresses if i press cancel or OK without filling anything into the text field. – Sim Jan 16 '18 at 14:28
  • 1
    @Sim I have added the check for empty string. Double check the indentation (ALL code at the same level within the if block, including providing enough white spaces in blank lines, beware of tabs) – JGH Jan 16 '18 at 15:13

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.