3

In QGIS 3.22 use the drag and drop designer to set up form.

The form are used for data entry but also with the Identify Features tool or with the attribute table form view to review data as form let you choose witch field to display, organise them in a logical manner and use widget to change how the value are displayed.

This work perfectly well in edit mode but when just interrogating data (so not in edit mode) some widget displays the attribute in medium grey on a light grey background (for exemple the drop down of a Value Map widget) while other have black text (for exemple text edit widget). This give a low legibility to the grey text attribute and create a false hierarchy to the point that some user may get the impression that the grey text is just filler text rather than an actual attribute they need to review (after all it is common practice on the web to pre-fill form with text like "username" and "password" to let you know what to type in).

See below what I mean:

  • edit mode on

Form in edit mode

  • edit mode off

Form not in edit mode

I think that being able to display attribute in a consistant way will provide a better experience for user so my question is: How to change the font color of the displayed attribute in a form when not in edit mode? (I'm avare that it's possible to change the label color and font but this has no impact on the attribute value)

1 Answer 1

4

I guess this will be a bit tricky to solve without a bit of effort. The only two things that come to my mind is using a ui-file designed in QT-Designer instead of the drag/drop form or a custom python function in the attribute form.

The second idea is described here:

enter image description here

enter image description here

Example Code for a Field "Name" as Combobox in the Form:

from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
 
nameField = None
myDialog = None

# based on https://woostuff.wordpress.com/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/ 

def openForm(dialog,layerid,featureid):
  global myDialog
  myDialog = dialog
  global nameField
  print(dialog.children())
  nameField = dialog.findChild(QComboBox,"Name")
  changeColor()
 
def changeColor():
    nameField.setStyleSheet("color: rgb(255, 0, 0);")

enter image description here

The downside of this approach is that you have to activate Macros in QGIS what I personally don't like due to security reasons, especially when opening QGIS projects from someone else.

Update1: If you want to style all Comboboxes in your form you can use:

from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
 
# based on https://woostuff.wordpress.com/2011/09/05/qgis-tips-custom-feature-forms-with-python-logic/ 

def openForm(dialog,layerid,featureid):
  all_combos = dialog.findChildren(QComboBox)
  for combo in all_combos:
    combo.setStyleSheet("color: rgb(255, 0, 0);")
2
  • I updated the answer above to show how to style all comboboxes in the dialog
    – Thomas B
    Commented Dec 6, 2023 at 15:21
  • 1
    You python solution work perfectly well, as the project will only by used internally activating macro for this one project will be acceptable for user
    – J.R
    Commented Dec 6, 2023 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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