I have a labelling issue in QGIS (using 3.4, but haven't tried in other versions). I have polygons for which I show an id number as well as some data information in the labels. I've used rule-based labelling for this: I set two labels and gave them different offsets to the centroid. This results in a two-line-label with on the top part the information you want to display and on the bottom part the id number of the polygon. This is when not using columns to define the X and Y position.

Sometimes the centroid is not where you want it to be, and so you can use X and Y columns to change the position of the label. Problem is that using these columns negates the offsets I set in the labels, so that they are renderend on top of each other. It doesn't help to make the offsets bigger: they just have no effect anymore, I assume because these settings do not affect the data defined label position.

Is there any way to define offsets to a data defined centroid for Polygons? Or does anyone have a different way to achieve the labels that I want without using offsets?

Example: I changed the position of the label of polygon 20 but now it's broken: enter image description here

I have two rule-based labels applying to all polygons: enter image description here

I have settings for offsets but these don't apply to the data-defined position and I seem to have no options to do that in that part of the settings: enter image description here

  • Try to set your offset as data defined and not by filling the box and see if it work better that way – J.R Dec 18 '19 at 9:58
  • It seemed a good suggestion to me, thanks! However, I cannot work it out, it does not seem to work. I can only assign string columns as the data defined offset, which doesn't make sense. Does anybody know how this works? I tried to figure it out by assigning a column and then moving the label, because usually then you will get the data defined value in the column. I hoped I could see the 'syntax' of it then, but it didn't work. I still hope someone has a solution... – Sanne de Bruin Dec 19 '19 at 12:30
  • You can add an offset to a data-defined position. Just use an expression like "X_coord"+10 where "X_coord" is the field where you store the label placement X coordinate, and 10 is the offset amount. – csk Dec 20 '19 at 20:44
  • Thanks for this suggestion. Another step closer, but unfortunately, it still does not work. I can indeed define the offset in the data-defined expression, and that works. But when I drag and drop the labels afterwards (say, because I've decided that it should go a little more to the right), the problem returns again because the drag and drop overrides the expression, not just the "X_coord" part, which is what I want. I can deal with this but the problem is I'm making stylesheets for other people to work with, who should be able to use them without to much effort... – Sanne de Bruin Jan 8 '20 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.