I need a tool which displays the sum of selected lines as I go in QGIS. Something like the Measure line-tool. I´m new to PyQGIS. The logic seems quite simple, I just don´t know where to start. I guess this could be a good starting point https://gis.stackexchange.com/a/211162/161290?
A possible solution is using the Field Calculator to create a label that show the sum of the selected lines.
In the lines layer create a label using the expression
array_sum(aggregate( layer:= 'line', aggregate:='array_agg', expression:=$length, filter:=is_selected()))
NB: To use
array_sum in the oldest QGIS versions you need to install the plugin arrayPlus.
This will create a label with the sum of the selected lines. It will appear in all the lines.
Since QGIS 3.10 in the label is integrated a Geometry Generator: you can use it to move the label to the centre of the screen or in a preferred position.
In the Layer Styling > Label Panel go to the Geometry Generator and use the variable
@map_extent_center and select the Point / Multipoint geometry option
If you want you can play with colour , background and offset to move your label. This is an example integrating the original expression with a text.
Here the modified expression:
concat( 'Total length: ', array_sum(aggregate( layer:= 'line', aggregate:='array_agg', expression:=(format_number($length,3)), filter:=is_selected())) ,' m')