6

By using "Identify Features" QGIS shows a form for viewing and editing attributes in the canvas.

Now I want to change the visible form elements dynamically by attribute value.

An example in pseudocode:

if attribute("status") = "closed":
  remove form element (attribute("status"))

In the layer properties there is the functionality of providing python code, but I have no clue how to access specific form elements. I thought it should be something like that (without conditional statement for testing), but without success:

from qgis.PyQt.QtWidgets import QWidget

def my_form_open(dialog, layer, feature):

status = dialog.findChild(QLabel,"status")
removeChild(status)

Could someone give me a hint how to access the form elements (dialog.findChild?) and change their behavior?

  • 1
    Is python a requirement or is a configuration only option also a possibility? qfield.org/docs/et/project-management/… – Matthias Kuhn Apr 25 at 12:17
  • Thank you. To be honest, I was not aware of that configuration option. For my purpose that's absolutely sufficient. Nevertheless I am more interested in a python-based solution...you could post your comment as an answer and I will mark it as an answer – pascatl Apr 25 at 13:38
3

QGIS can do that without any Python code as well.

The following excerpt is from the QField documentation.

Groups can be hidden based on an expression. This is useful when certain attributes are only required under certain conditions. As an example imagine mapping trees. Some of them might have a disease and you have a list of possible diseases. To not clutter the form with many fields, make a group and configure a visibility expression for a group “diseases”. Add a field “disease” with a checkbox. Only when the checkbox is checked, the list of diseases will be available to the user.

The following configuration interface shows up if using the drag and drop designer and double clicking a group.

enter image description here

3

The following works:

from PyQt5.QtWidgets import QLineEdit, QWidget

def my_form_open(dialog, layer, feature):
    geom = feature.geometry()
    control = dialog.findChild(QLineEdit, "id") #works with QWidget as well
    control.hide()

But note, that the label does not hide, which makes the dialog look little weird.

I tried to access and hide the label via

    label = dialog.findChild(QLabel, "id")
    label.hide()

but this does not work for some reason.

(If I figure this out I'll update my answer accordingly...)

  • Is there a setVisible() parameter? I.e. label.setVisible(False)? – Joseph Apr 26 at 9:21
  • Does that work control.buddy().hide()? – Matthias Kuhn Apr 26 at 10:04
1

In the QGIS-Function editor (QGIS 3.8) it`s possible to create an own function, e.g.:

from qgis.PyQt.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis.PyQt.QtSql import QSqlDatabase, QSqlQuery
from PyQt5.QtWidgets import QTableView, QApplication
import sys
from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def datenbank(uebergabe,feature, parent):
    db = QSqlDatabase.addDatabase("QOCISPATIAL")
    db.setPort(1521);
    db.setHostName("stadt-bbbbb")
    db.setDatabaseName("cccc")
    db.setUserName("ddddddd")
    db.setPassword("eeee")
    if not db.open():
        return("wrong")
    #else:
        #return("true")

    queryx = QSqlQuery(db)
    queryx = db.exec_('SELECT output_z from vvvvvv where ww_section.fid_aw_von='+str(uebergabe)+' or ww_section.fid_aw_bis='+str(uebergabe))

    result2 = []
    i=0
    html=""
    while queryx.next():
        #return("super")
        record = queryx.record()
        result2.append(record.value(0))
        html+="<input type=text id="+str(i)+" value="
        html+=str(result2[i])
        html+=">"
        html+="</br>"
        i=i+1
    db.close()
    return (html)

This will put out dynamically html-code (with values) within the QGIS-form.

Of course that function must be implemented via the drag-and-drop form editor as a html-element in the form, like:

<script>document.write(expression.evaluate("datenbank( \"FID\" )"));</script>

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.