I have a project on QGIS 3.16 with several layouts. I would like to change the scalebar width according to the value of a Python variable, either a fixed sized of 1000 or 500 units (for instance).
First, I tried to do it without the Python variable, by using the value of 1000 units. I think the following code I use try to create a new scalebar, instead of calling the existing one. Here, the name of the actual layout is 'cumul_eoliennes':
(once the QGIS projet is opened)
project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
composeur_eol = 'cumul_eoliennes'
layout_eol= projectLayoutManager.layoutByName(composeur_eol)
scalebar=QgsLayoutItemScaleBar(layout_eol)
scalebar.setHeight(1)
# scalebar.height() gives the default value of 3 mm
scalebar.setFixedSize(1000)
The last code gives :
TypeError: QgsLayoutItemScaleBar.setFixedSize(): argument 1 has unexpected type 'int
In conclusion, I meet two problems:
- it seems I am not able to call the actual scalebar of a layout
- I don't know how to change the "FixedSize" of a scalebar
[EDIT about the FixedSize] I am looking for the scalebar fixed size function like below: