"Will the scale bar be "at scale" ?"
Your scale bar's scale will be relative to an existing composer map item that is assigned to it: QgsComposerScaleBar.setComposerMap(mymap_item_obj_here)
. So you will need to create the composer map item first, so it's scale can be referenced by the scale bar.
"I cannnot find anything from that page ScalBar class"
I found almost all the necessary functions to create the custom scale bar you want on that page.
Once the composer map reference is set, all other QgsComposerScaleBar attributes can be set using the class's public member functions, or those functions inherited by the scale bar's base class, QgsComposerItem, like setItemPosition().
Base class inheritance diagram
I guessing here, but for setting the opacity for the background to zero, you will probably want to set the QBrush used in QgsComposerScaleBar.setBrush(QBrush) to have a QColor with an alpha of 0.
EDIT (response to your further questions/clarifications):
Regarding the scale back groundI have been trying the following :
brush = ScaleBar.brush()
brush.setColor(QColor(255, 0, 0, 0))
QgsComposerScaleBar.setBrush(brush)
I was incorrect in assuming the QBrush in question belonged to the encompassing frame. The brush is actually the color for the scale bar's font. The frame and its background are part of the inherited QgsComposerItem class. To effectively work with its methods of a similar name (setBrush() in this example), you need to access it via Python's super() function (using your ScaleBar object here):
scalebaritem = super(QgsComposerItem, ScaleBar)
scalebaritem.setBrush(QColor(0,0,0,0))
This will reference the super class of the object, and set it to transparent black, much like setting parameters in the General options section for a composer item.
I could not change the font size !
ScaleBar.font().toString()
PyQt4.QtCore.QString(u'Ubuntu,12,-1,5,50,0,0,0,0,0')
>>> ScaleBar.font().setPointSize(8)
>>> ScaleBar.font().pointSize()
12
You need to create a QFont, set its size, then set the scale bar's font to it. You can begin by pulling in the QFont from the scale bar.
f = ScaleBar.font()
f.setPointSize(8)
ScaleBar.setFont(f)
Best Answer
You have two option to create a scale bar
What you do "normally" you fix the width of your segment in your map unit according to the scale you use and to have easy to use number "25 / 50 / 75 /100". If your map scale change your scale bar length in the print composer.
OR
You can set the segment size in your layout, this is the
Fit segment width
option. If you set it to be 50 mm long in your layout no matter the scale used then you will get value that are not "round" (see picture below) that will be difficult to use for the map user and ugly (my opinion)To do it better you have three different solutions :
get margin around feature
but use thepredefined scale (best fit)
option. Then set your predefined scale to match most atlas page.UPDATE FOR YOUR USE CASE
To use the solution number 1 you have to set the two values in yellow. The one in the top is used as the minimal dimension of a segment in your layout and the one below is the maximal size for your segment.
In your screenshot, we see that your legend element is 20 mm long. As you have two segments, the max size for each segment should be 10 and the minimal size should be 6 mm to keep segment visible. QGIS will choose the segment size to get a round number to show in map unit.