Is it possible to get the decorator scale bar onto the map canvas inside a standalone PyQGIS application? It is possible to get a scale bar on the QGIS Desktop version by View -> Decorations -> Scale Bar
I saw this post: Access / modify QGIS copyright decoration through python but this is solely only for the copyright decorator. Was wondering if there is anything similar for the scale bar.
1 Answer 1
Using the same method as in the answer you added,
I simply adjusted to PyQt5 and guessed base on the example that this will work.
It seems this works only in projects which have already been saved (for me it crashed the untitled project i was in) but works pretty well.
from PyQt5.QtCore import QSettings
QgsProject.instance().writeEntry( "ScaleBar", "/Color" , "#ff9e00" ); #Can also be rgba 255,158,0,255
QgsProject.instance().writeEntry( "ScaleBar", "/MarginH" , 0 );
QgsProject.instance().writeEntry( "ScaleBar", "/MarginV" , 0 );
QgsProject.instance().writeEntry( "ScaleBar", "/MarginUnit" , "MM" );
QgsProject.instance().writeEntry( "ScaleBar", "/Style" , 3 ); # 0 Tick Down, 1 Tick Up, 2 Bar, 3 Box,
QgsProject.instance().writeEntry( "ScaleBar", "/Placement" , 0 ); # Bottom Left
QgsProject.instance().writeEntry( "ScaleBar", "/PreferredSize" , 30 );
QgsProject.instance().writeEntry( "ScaleBar", "/Enabled" , True );
#Save Project
QgsProject.instance().write()
extent = iface.mapCanvas().extent()
#Reload project
def set_extent():
iface.mapCanvas().setExtent(extent)
iface.projectRead.disconnect(set_extent)
print('Loaded using new copyright label')
filename = QgsProject.instance().fileName()
iface.projectRead.connect(set_extent)
iface.addProject(filename)
If you want to get the other options you can save your project as Qgs and find all the options there inside the ScaleBar
node.
The font properties are defined within an inner TextFormat
node.
enter image description here
-
is it possible to use this method on a standalone pyqgis application on a custom canvas? Because as from what I know, standalone applications does not have the iface interface.Sean Ang– Sean Ang2020年10月26日 11:01:52 +00:00Commented Oct 26, 2020 at 11:01
-
I'm not sure how you would get that standalone pyqgis canvas but iface is a pointer to a
qgis.gui.QgisInterface
object. You can try using that.Dror Bogin– Dror Bogin2020年10月26日 11:24:09 +00:00Commented Oct 26, 2020 at 11:24 -
gis.stackexchange.com/questions/340458/… The answer from this thread says that theres no iface in standalone QGIS applications.Sean Ang– Sean Ang2020年10月26日 11:51:44 +00:00Commented Oct 26, 2020 at 11:51
Explore related questions
See similar questions with these tags.