3

I'm using QGIS 3.0.1 and I would like to change the font color of some attribute fields with a changing value in attribute table (depends on which value was entered). My idea is to use signal "attributeValueChanged" but I can't find any information about how I can change font color or how I can access it via python. Does QGIS 3.0.1 / Python support something like this or rather not?

Example:

enter image description here

  • 1
    Are you trying to do it with python programming only ? if not using the conditional formatting button (last button on top of attribute table) do the trick – J.R Mar 29 '18 at 14:29
  • I work on a plugin and I would like to do it in python. Is it possible to define the conditional formatting in python? – DG85 Mar 29 '18 at 14:37
  • 1
    Yes, you can define conditional formatting using the Python API. Have a look to qgis.org/api/classQgsConditionalLayerStyles.html I will try later an answer – etrimaille Apr 2 '18 at 12:22
1

Thats my code how it works for me for a very simple case.

    if self.isSupported(aLayer):
     provider = aLayer.dataProvider()
     columns = provider.fields()
     for c in columns:
      name = str(c.name())
      c_styles = aLayer.conditionalStyles()
      f_styles = c_styles.fieldStyles(name)
      list = []
      style1 = QgsConditionalStyle("@value = 'invalid'")
      color = QColor(255,0,0)
      style1.setTextColor(color)
      list.append(style1)
      c_styles.setFieldStyles(name, list)
0

It is possible to change the colour of label text by attribute using data defined override on 'color'. For example this expression works for me in QGIS 3:

CASE
 WHEN "local_type" = 'Town' THEN color_rgb(255,0,0)
 WHEN "local_type" = 'City' THEN  color_rgb(34,139,34)
 WHEN "local_type" = 'Village' THEN  color_rgb(0,0,205)
ELSE  color_rgb(80, 80, 80) END

How you would go about changing the attribute is not something I can help you with I'm afraid.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.