Pyqgis Set the field value of a dynamically created vector layer in QGIS 3 before user saves to database

by user3398094   Last Updated August 14, 2019 15:22 PM

I am quite new to both QGIS and Python. I have inherited a project that needs to be upgraded from using QGIS 2.0 DUFOUR to 3.8 Zanzibar.

The way it works is, there is a separate application written in On one of the web pages there is a button called 'Digitize'.

  • When the user clicks this it saves a file with the details of the current record, onto the web server.

  • The user then opens QGIS via a shortcut on their desktop. The QGIS app is locked down with only minimal functionality.

  • When the project has finished loading, a plugin button becomes available on the toolbar. This button creates a new Vector Layer by connecting to a mssql database.

    • The vector layer name is based on dynamic values in the file on the server, so it changes each time.
    • The layer is also removed every time QGIS is started.
    • It displays the features/polygons that have already been added (if there are any).
  • The user clicks the pencil to begin editing.
  • The user draws a polygon.
  • The user clicks save.

  • The user is always prompted to enter an id which the record needs as a foreign key in the database. However, the user will never have access to this ID which is a long string e.g. '0000000001-0000000000000264100' that is hidden in the code.

I can get this value in the code from the server file (see sublayerref below). My problem is I cannot find a way to set this programmatically. Does anybody know how to do this?

Here is the code so far, the best I can come up with at the moment is using the signal 'editingStarted', but I haven't found any success setting the value of 'ForeignKeyFieldId' field. Any help is much appreciated.

    def feedback2():
    print("XXXFEEDBACK2 ACTIVATED:" + sublayerref)
    df = QgsDefaultValue('var("varsublayerref")', True)
    for field in vlayer.fields():
        print("LayerField:" +
        if == "ForeignKeyFieldId":
            print("Job done")
crs = QgsCoordinateReferenceSystem("EPSG:27700")
uri = QgsDataSourceUri()
uri.setConnection(servername, portnumber, databasename, logon, password)
uri.setDataSource( schemaname, tablename, geometryfieldname,  sublayerid + " = '" + sublayerref + "'" ) 
uri.setSrid("EPSG: 27700")
primaryKeyColumn = "RecordId"

symbol = QgsFillSymbol.createSimple({'line_style': 'dash', 'color': layercolour })
vlayer = QgsVectorLayer()
vlayer.setCustomProperty("labeling", "pal")
vlayer.setCustomProperty("labeling/enabled", "true")
vlayer.setCustomProperty("labeling/isExpression", True)
vlayer.setCustomProperty("labeling/fontFamily", "Arial")
vlayer.setCustomProperty("labeling/fontSize", "10")
vlayer.setCustomProperty("labeling/fontWeight", "2")
vlayer.setCustomProperty("labeling/fontBold", True)
vlayer.setCustomProperty("labeling/placement", "1")
vlayer.setCustomProperty("labeling/fieldName", "'" + labelprefix + " ' || " + '"' + pkfieldname + '"')
vlayer = iface.addVectorLayer(uri.uri(), layername, 'mssql')
vlayer = iface.activeLayer()
vlayer.editingStarted.connect(feedback2) ##testcode

Related Questions

Counting number of polygons inside zone using QGIS?

Updated August 06, 2019 11:22 AM

Vector Grid, Graphical Modeler & Input Extent

Updated August 22, 2017 13:22 PM