I'm writing a plugin that extracts some data from a Postgres database, saves a point shapefile of the data and displays it in QGIS. As part of that process, I want to generate a QML style file.
I have this basically working - with the exception of the label placement. I want the label placed NE (above and right) of the point, offset X=1, Y=0.
I have the following code. It all works - but the label is placed 'Around point' rather than offset up and to the right.
#Add labels.
layer_settings = QgsPalLayerSettings()
layer_settings.fieldName = "gr"
layer_settings.isExpression = True
layer_settings.OffsetType = QgsPalLayerSettings.FromPoint
layer_settings.OffsetQuad = 'NE'
layer_settings.OffsetXY = '1,0'
layer_settings.OffsetUnits = QgsUnitTypes.RenderUnit.RenderMillimeters
layer_settings.enabled = True
text_format = QgsTextFormat()
text_format.setFont(QFont("Arial Black", 6))
text_format.setSize(6)
layer_settings.setFormat(text_format)
labeling = QgsVectorLayerSimpleLabeling(layer_settings)
layer.setLabelsEnabled(True)
layer.setLabeling(labeling)
layer.triggerRepaint()
# Save the style file for future use in QGIS.
outputFile = os.path.join(self.SearchOutputFolder, searchCategoryFileName) + '.qml'
layer.saveNamedStyle(outputFile)
I've had to guess how to use OffsetQuad and OffsetXY. I just can't seem to find any documentation on how to call/set them.