Skip to main content
1 of 4
Kai_B
  • 31
  • 4

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

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 asp.net. 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 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:" + field.name())
    if field.name() == "ForeignKeyFieldId":
        field.setDefaultValueDefinition(df)
        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") uri.setUseEstimatedMetadata(True) uri.setWkbType(QgsWkbTypes.Polygon) primaryKeyColumn = "RecordId" uri.setKeyColumn(primaryKeyColumn)

symbol = QgsFillSymbol.createSimple({'line_style': 'dash', 'color': layercolour }) symbol.setOpacity(0.5) vlayer = QgsVectorLayer() vlayer.setCrs(crs) 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.setOpacity(0.5) vlayer.updateExtents() vlayer = iface.addVectorLayer(uri.uri(), layername, 'mssql') vlayer.renderer().setSymbol(symbol) vlayer = iface.activeLayer() vlayer.editingStarted.connect(feedback2) ##testcode }

Kai_B
  • 31
  • 4