I have an attribute form created with QGIS 'Drag and drop Designer' that looks like this:

enter image description here

As can be seen, the attribute aliases (the labels on the left) are little longer because of some explanatory text in the round brackets.

It might be a matter of taste and a matter of readability as well, but is there a way to right-align theses labels to make them stand near the related input widet? Can this be achieved with QGIS standard tools or do I have to create an ui file with the QtDesigner?

  • 2
    I wouldn't call it a matter of taste, but a matter of readability. But I think you should use abbreviations first, e.g. "Lfd.Nr. (straßenbez.)", "Drehung (UZS, 0=N)[°]" or "Höhe [m]". Then reassess the need for alignment? – Erik Mar 3 at 9:14
  • @Erik An approach worth discussing with the customer. – Jochen Schwarze Mar 3 at 9:18
  • 1
    For me, you have to do it under QtDesigner, but it's very simple if the fields have same names as the atlas_punkte table. Maybe, if the fields names are always the same, rename other fields with spaces before to simulate a right align ? – J. Monticolo Mar 3 at 9:36

I found a solution that makes use of a python function that is called when the dialog opens:

enter image description here

# -*- coding: utf-8 -*-

from qgis.PyQt.QtWidgets import QWidget, QLineEdit, QLabel
from qgis.PyQt.QtCore import Qt

def atlas_punkte_init_values(dialog, layer, feature):
    labels_to_align = ['hoehe_druck', 'breite_druck', 'rot_druck', 'scale_druck']
    labels = dialog.findChildren(QLabel)
    for l in labels:
            if l.buddy().objectName() in labels_to_align:
                l.setAlignment(Qt.AlignRight | Qt.AlignVCenter)

By providing a list of attruibute names the labels can be aligned selectively, and the use of Qt.AlignVCentermakes the alignment look even fancier (try it without :-)

Result (note the labels in the 'Druck'-box):

enter image description here

By accessing the methods of QLabel each kind of style modification should be possible in the attribute form labels (and other elements).

| improve this answer | |

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.