2

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.

MarcM
1,1865 silver badges20 bronze badges
asked Oct 26, 2020 at 8:57

1 Answer 1

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

answered Oct 26, 2020 at 10:23
3
  • 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. Commented 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. Commented 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. Commented Oct 26, 2020 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.