Bug report #6468

loadNamedStyle does not apply labelfield

Added by Rafael Varela over 7 years ago. Updated over 4 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Project Loading/Saving
Affected QGIS version:1.8.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:worksforme
Crashes QGIS or corrupts data:No Copied to github as #:15701

Description

My setup: QGIS 1.8.0 on Debian Squeeze (1.8.0~squeeze1 package) I'm trying to apply a style to a in-memory layer from a .qml file.

If I apply the style with the loadNamedStyle function, the style is correctly applied, with the exception of the labelfield (all the the labels of the features show the default value set in the style). If I go through the GUI, I can verify that the field label is not set.

To rule out problems with the .qml file, loading the same .qml file in the layer properties dialog correctly sets the label with the contents of the desired field.

Here's the code I'm using for testing:

puntoA = QgsPoint(-7.45,42.45)

vl = QgsVectorLayer("Point", "puntos_en_memoria", "memory")
pr = vl.dataProvider()

vl.startEditing()

pr.addAttributes( [ QgsField("name", QVariant.String),
                QgsField("size", QVariant.Double) ] )

fet = QgsFeature()
fet.setGeometry( QgsGeometry.fromPoint(puntoA) )

fet.setAttributeMap( { 0 : QVariant("puntoA"),
                   1 : QVariant(20) } )

pr.addFeatures( [ fet ] )
vl.loadNamedStyle("/path_to_styles/puntos_en_memoria.qml")
vl.commitChanges()
vl.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(vl)

and this is the content of the .qml file:

<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="1.8.0-Lisboa" minimumScale="0" maximumScale="1e+08" minLabelScale="0" maxLabelScale="1e+08" hasScaleBasedVisibilityFlag="0" scaleBasedLabelVisibilityFlag="0">
  <transparencyLevelInt>255</transparencyLevelInt>
  <singlesymbol>
    <symbol>
      <lowervalue></lowervalue>
      <uppervalue></uppervalue>
      <label>Nodos</label>
      <pointsymbol>hard:circle</pointsymbol>
      <pointsize>4</pointsize>
      <pointsizeunits>pixels</pointsizeunits>
      <rotationclassificationfieldname></rotationclassificationfieldname>
      <scaleclassificationfieldname></scaleclassificationfieldname>
      <symbolfieldname></symbolfieldname>
      <outlinecolor red="0" blue="0" green="0"/>
      <outlinestyle>SolidLine</outlinestyle>
      <outlinewidth>0.26</outlinewidth>
      <fillcolor red="216" blue="88" green="73"/>
      <fillpattern>SolidPattern</fillpattern>
      <texturepath></texturepath>
    </symbol>
  </singlesymbol>
  <customproperties/>
  <displayfield>name</displayfield>
  <label>1</label>
  <labelfield>name</labelfield>
  <labelattributes>
    <label fieldname="name" text="Desconocido"/>
    <family fieldname="" name="Sans"/>
    <size fieldname="" units="pt" value="12"/>
    <bold fieldname="" on="0"/>
    <italic fieldname="" on="0"/>
    <underline fieldname="" on="0"/>
    <strikeout fieldname="" on="0"/>
    <color fieldname="" red="0" blue="0" green="0"/>
    <x fieldname=""/>
    <y fieldname=""/>
    <offset x="0" y="0" units="pt" yfieldname="" xfieldname=""/>
    <angle fieldname="" value="0" auto="0"/>
    <alignment fieldname="" value="center"/>
    <buffercolor fieldname="" red="255" blue="255" green="255"/>
    <buffersize fieldname="" units="pt" value="1"/>
    <bufferenabled fieldname="" on=""/>
    <multilineenabled fieldname="" on=""/>
    <selectedonly on=""/>
  </labelattributes>
  <edittypes>
    <edittype type="0" name="N"/>
    <edittype type="0" name="age"/>
    <edittype type="0" name="alt"/>
    <edittype type="0" name="i"/>
    <edittype type="0" name="lat"/>
    <edittype type="0" name="lon"/>
    <edittype type="0" name="name"/>
    <edittype type="0" name="nodlab"/>
    <edittype type="0" name="pini"/>
    <edittype type="0" name="pmin"/>
    <edittype type="0" name="size"/>
    <edittype type="0" name="theta"/>
  </edittypes>
  <editform></editform>
  <editforminit></editforminit>
  <annotationform></annotationform>
  <attributeactions/>
</qgis>

layers.png - Layer labels after applying style using loadNamedStyle (50 KB) Rafael Varela, 2012-10-08 12:44 PM

layer_properties.png - Point layer properties (67.8 KB) Rafael Varela, 2012-10-08 12:44 PM

History

#1 Updated by Rafael Varela over 7 years ago

I repeated the test with a nightly build of QGIS (1.9.0-Master). After updating the .qml file, the style is applied correctly

#2 Updated by Giovanni Manghi over 7 years ago

  • Status changed from Open to Feedback

Rafael Varela wrote:

I repeated the test with a nightly build of QGIS (1.9.0-Master). After updating the .qml file, the style is applied correctly

so this is fixed in master?

#3 Updated by Rafael Varela over 7 years ago

Giovanni Manghi wrote:

so this is fixed in master?

Initially, it seemed to me that it was fixed, but today I repeated the test and the problem is still there.

This time, although, I noticed there's a difference: the style enables both the label taken from the field and the label from the default value simultaneously. If I set the style using the GUI, then the label gets duplicated (the value is the one coming from the field declared in the style).

In the attached screenshot you can see the two labels just after loading the style with the loadNamedStyle function ("desconocido" is the default label value in the style).

I seems that I didn't realize that the extra label was coming from the same layer. Sorry for the misinformation.

#4 Updated by Rafael Varela over 7 years ago

Well, I realized that in master there are two tabs in the layer properties windows (one of it marked as deprecated), so the extra label was coming from a duplicate label settings in the style definition (although my file was generated by the 1.8 version with only one label setting).

After disabling the deprecated label setting and saving a new style file, now the loadNamedStyle function sets the style correctly.

#5 Updated by Moo Lambda about 7 years ago

I am afraid I am encountering this bug on qgis 1.9 nightly for debian wheezy amd64. Old qml styles made in 1.7 that worked in 1.8 now do not apply the label field. I can understand that, because of the change to the new labeling system. Unfortunately, when I save and then load a style using the new labeling (either .qml or .sld), the label field and other settings are still not applied.

Since 1.9 is definitely for testing, I can understand. Unfortunately, I have so many old styles saved (they don't work, because the old label settings are completely ignored) that even though I can port them over to the new labeling, I can't re-port them over every time I open a project. I hope this is the right bug to report this to; it may also be related to 6222, but I don't know. The line styles and colors seem to work when saving and loading. It's just the label field (new or old labeling) properties that are not being loaded.

The new labeling is really looking good to me, so I hope this can be fixed soon even if it's only for the new labeling.

#6 Updated by James Stott over 6 years ago

  • Target version set to Version 2.0.0

I have this problem also. The labels do not get applied to my maps when using a standalone python script using the line:

vLayerName.loadNamedStyle("current_area_style.qml")

QGIS master on Windows 7.

QGIS 1.8 and 1.9 restore the labels correctly from with QGIS, but not via python.

#7 Updated by Jürgen Fischer almost 6 years ago

  • Category set to Project Loading/Saving

#8 Updated by Giovanni Manghi almost 6 years ago

still true on latest qgis releases?

#9 Updated by Jürgen Fischer almost 6 years ago

  • Target version changed from Version 2.0.0 to Future Release - Lower Priority

#10 Updated by Jürgen Fischer over 5 years ago

James Stott wrote:

I have this problem also. The labels do not get applied to my maps when using a standalone python script using the line:

vLayerName.loadNamedStyle("current_area_style.qml")

QGIS master on Windows 7.

QGIS 1.8 and 1.9 restore the labels correctly from with QGIS, but not via python.

did you add the layer to the map layer registry before loading the style? After the edit widget refactoring this is a requirement for having the widget restored.

#11 Updated by Giovanni Manghi over 4 years ago

  • Resolution set to worksforme
  • Status changed from Feedback to Closed

closing for lack of feedback.

Also available in: Atom PDF