3

I have a custom form with some validation.

One these validation is if the value of attribute is NULL. The value of NULL representation on the QGIS is float according of the Configure->Options.

How to check if the value on the QLineEdit is NULL according with the QGIS NULL value?

When I know QGIS representation no problems, but if anybody changes this parameter... oh, oh!

I´m try this way:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from pyspatialite  import dbapi2 as sqlite

fgeocodigo = None
fsittip = None
fbairro = None
fasubn  = None
fuca    = None
faglom  = None
faui    = None 
fti     = None
ftq     = None
feuv    = None
fedoc   = None
feno    = None
fecol   = None
st_ok = None
fDlg = None
ls = None

def formOpen(dialog,layerid,featureid):
    global fDlg
    fDlg = dialog
    global fgeocodigo
    global fsittip
    global fbairro
    global fasubn
    global fuca
    global faglom
    global faui
    global fti
    global ftq
    global feuv
    global fedoc
    global feno
    global fecol

    global buttonBox

    global ls

    fgeocodigo = dialog.findChild(QLineEdit,"cd_geocodigo")
    fsittip = dialog.findChild(QLineEdit,"cd_sit_tip")
    fbairro = dialog.findChild(QLineEdit,"bairro")
    fasubn = dialog.findChild(QLineEdit,"asubn")
    fuca = dialog.findChild(QLineEdit,"uca")
    faglom = dialog.findChild(QLineEdit,"aglom")
    faui = dialog.findChild(QLineEdit,"aui")
    fti = dialog.findChild(QLineEdit,"ti")
    ftq = dialog.findChild(QLineEdit,"quilom")
    feuv = dialog.findChild(QLineEdit,"ctg_euv")
    fedoc = dialog.findChild(QLineEdit,"ctg_edoc")
    feno = dialog.findChild(QLineEdit,"ctg_eno")
    fecol = dialog.findChild(QLineEdit,"ctg_ecol")
    buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")

    file_db_n = QgsProject.instance().readPath("./") + "\\DB\\" + "db.sqlite"
    conn = sqlite.connect(file_db_n)
    cur = conn.cursor()

    cur.execute('SELECT cd_sit_tip FROM T_Sittipo')
    st = cur.fetchall()
    ls = []
    for s in st: ls.append(s[0])


    fgeocodigo.textChanged.connect(valida_form)
    fsittip.textChanged.connect(valida_form)
    fasubn.textChanged.connect(valida_form)
    fbairro.textChanged.connect(valida_form)
    fuca.textChanged.connect(valida_form)
    faglom.textChanged.connect(valida_form)
    faui.textChanged.connect(valida_form)
    fti.textChanged.connect(valida_form)
    ftq.textChanged.connect(valida_form)
    feuv.textChanged.connect(valida_form)
    fedoc.textChanged.connect(valida_form)
    feno.textChanged.connect(valida_form)
    fecol.textChanged.connect(valida_form)


def valida_form(text):
    flag = True

    #---[Valida geocodigo]
    if  len(str(fgeocodigo.text())) != 15:
        fgeocodigo.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
        flag = False
    else:
        fgeocodigo.setStyleSheet("")

    #---[Valida sittip]
    if  str(fsittip.text()) not in ls:
        fsittip.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
        flag = False
    else:
        fsittip.setStyleSheet("")

    #---[Valida asubn]
    if  len(str(fsittip.text())) == 2:
        if  str(fsittip.text()) in ['11', '31', '41'] and ((str(fasubn.text()) is None) or (str(fasubn.text()) == '')):
            fasubn.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        elif str(fsittip.text()) not in ['11', '31', '41'] and not ((str(fasubn.text()) is None) or (str(fasubn.text()) == '')):
            fasubn.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            fasubn.setStyleSheet("")

    #---[Valida bairro]
    if  len(str(fsittip.text())) == 2:
        if  str(fsittip.text())[0] not in ['1', '3', '4'] and not ((str(fbairro.text()) is None) or (str(fbairro.text()) == '')):
            fbairro.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            fbairro.setStyleSheet("")


    #---[Valida aglom]
    if  len(str(fsittip.text())) == 2:
        faglom.setStyleSheet("")
        if  str(fsittip.text())[0] in ['5', '6', '7'] and ((str(faglom.text()) is None) or (str(faglom.text()) == '')):
            faglom.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False

        if  str(fsittip.text()) in ['10','11','12','13','14','16','18','20','22','23','24','25','26','27','30','31','32','33','34','36','37','38','40','41','42','43','44','46','47','48','68','80','82','83','84','86','87'] and \
            not ((str(faglom.text()) is None) or (str(faglom.text()) == '')):
            faglom.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False

        if  str(fsittip.text()) in ['28','88'] and ((str(faglom.text()) is None) or (str(faglom.text()) == '')):
            faglom.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False

    #---[Valida aui]
    if  len(str(fsittip.text())) == 2:
        if  str(fsittip.text()) in ['30', '31', '32', '33', '35', '36', '37', '39'] and ((str(faui.text()) is None) or (str(faui.text()) == '')):
            faui.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        elif str(fsittip.text()) not in ['30', '31', '32', '33', '35', '36', '37', '39'] and not ((str(faui.text()) is None) or (str(faui.text()) == '')):
            faui.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            faui.setStyleSheet("")

    #---[Valida ti]
    if  len(str(fsittip.text())) == 2:
        if  str(fsittip.text())[1] == '5' and ((str(fti.text()) is None) or (str(fti.text()) == '')):
            fti.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        elif str(fsittip.text())[1] != '5' and not ((str(fti.text()) is None) or (str(fti.text()) == '')):
            fti.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            fti.setStyleSheet("")

    #---[Valida tq]
    if  len(str(fsittip.text())) == 2:
        if  str(fsittip.text())[1] == '9' and ((str(ftq.text()) is None) or (str(ftq.text()) == '')):
            ftq.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        elif str(fsittip.text())[1] != '9' and not ((str(ftq.text()) is None) or (str(ftq.text()) == '')):
            ftq.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            ftq.setStyleSheet("")

    #---[Valida qtd dom]
    if  len(str(fsittip.text())) == 2:
        if  len(str(feuv.text())) == 0:
            feuv.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            feuv.setStyleSheet("")

        if  len(str(fedoc.text())) == 0:
            fedoc.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            fedoc.setStyleSheet("")

        if  len(str(feno.text())) == 0:
            feno.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            feno.setStyleSheet("")

        if  len(str(fecol.text())) == 0:
            fecol.setStyleSheet("background-color: rgba(255, 100, 100, 150);")
            flag = False
        else:
            fecol.setStyleSheet("")

    buttonBox.button(QDialogButtonBox.Ok).setEnabled(flag)

I see this Nathan's post but I cannot implement.

1 Answer 1

3

If I understand well, you want to check for NULLs when the custom value for NULL is changed by the user from the Settings.

In this case, you may simply use:

from qgis.core import *
if <what you want to check> == qgis.core.NULL:
    # do something

The above check should compare the input with the value that the user set for the NULLs.

14
  • Hi @mgri, thank you. I've tested and am not sure but it looks like the 'qgis.core,NULL' contains a constant NULL value and does not reflect a change made in the settings. It did not work for me.
    – Sassá
    Commented Apr 3, 2017 at 16:49
  • @Sassá hmm, that's weird. I never had the need to use this kind of conditionals (so maybe my answer was wrong), but I tried to change the default NULL value with another one and it worked... Are you sure you applied the setting? Is there a specific case which didn't work for you? Did you try to restart QGIS after the edit on settings and before running the code?
    – mgri
    Commented Apr 3, 2017 at 16:54
  • thanks again. If I change the settings with i.e.: aaa. On the form ok, but the contents of qgis.core.NULL is NULL. On the console: > print qgis.core.NULL -> NULL. The comparison also fails.
    – Sassá
    Commented Apr 3, 2017 at 16:59
  • I think that the output should be always NULL because it's the core part in which the NULL value is stored (so, te print will be NULL if you set 'NULL', 'aaa', 'bbb' and so on as default NULL value). If you are looking for a specific value, instead you need to call the feature attribute (but it doesn't seem what you are looking for).
    – mgri
    Commented Apr 3, 2017 at 17:12
  • Yeah @mgri, I think so too. What I need is to retrieve the value entered in the settings to compare with the value of the qlineedit. I'm afraid someone will change that value ...
    – Sassá
    Commented Apr 3, 2017 at 17:40

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.