My QGIS 3.40.14/Python 3.12.12 code is creating a memory layer with a bunch of point features:
fields = QgsFields()
fields.append(QgsField('number', QVariant.Int)) #number should be unique within a line
fields.append(QgsField('linename', QVariant.String)) #Same name for all points in a line
fields.append(QgsField('date', QVariant.Int))
fields.append(QgsField('time', QVariant.Int))
self.vectorlayer = QgsMemoryProviderUtils.createMemoryLayer("points", fields, Qgis.WkbType.Point, self.epsg)
for p in points:
pointfeature = QgsFeature(fields)
pointfeature.setGeometry(QgsGeometry.fromPoint(QgsPoint(p.x, p.y, p.z)))
pointfeature.setAttributes([p.number, p.linename, p.date, p.time])
...
When the map isn't zoomed in, it's hard to click on a specific point, so I usually right-click and pick the point I want to click on in the menu. The problem: For some reason, it sets the "linename" as a default value, and since it's the same for all points within a line, it's impossible to distinguish between the points in that menu. If I remove this String attribute, then it uses the first one in the list, which is the number.
The "Identify Results" window looks like this:
How do I set the number as the top attribute for this specific layer instead?
This answer explains how to choose a default field in the layer settings, but I want to set it in code.
I already tried this, as suggested here, but it didn't change anything.
defval = QgsDefaultValue()
defval.setExpression("'number'")
self.vectorlayer.setDefaultValueDefinition(0, defval)


