1

I am trying to create a custom form for QGIS 2.0 with comboboxes containing an "other" category. I wonder how to provide a line edit to enter info replacing the "other" choice if chosen. Not sure if I can sync a combo box and a line edit at the same time under field properties.

In pseudo code:

myfield = dialog.findChild(QComboBox, "Myfieldname")
otherfield = dialog.findChild(QLineEdit, "Myfieldname")

if myfield.currentIndex() == -1:
    set myfield to otherfield.text()

This thread's idea seems very similar, but I'm having trouble adapting it: How to automatically populate fields instantly?

my custom form code so far:

Naturefield = None
Nat_autrefield = None

def formOpen(dialog, layerid, featureid):
    global myDialog
    myDialog = dialog
    global Naturefield, Nat_autrefield
    Naturefield = dialog.findChild(QComboBox, "Nature")
    Nat_autrefield = dialog.findChild(QLineEdit, "Nat_autre")
    Naturefield.currentIndexChanged.connect(autreChanged)


def autreChanged(value):
    #intending to take on form of line edit only if 'Autre' chosen
    if Naturefield.currentText() == 'Autre':
        Naturefield.setLineEdit(Nat_autrefield)

setLineEdit() currently transforms my combo box to make the list editable for all choices if 'autre' is chosen and the combo box stays a line edit once 'autre' is chosen--not exactly what i'm aiming for. Instead, I'd like the combo box to receive the input from a line edit only if 'autre' is chosen, and revert to normal combo box if other choices are chosen. This is all in the interest of data quality control.

I'm new to QtDesigner and Python.

1 Answer 1

1

I found the solution and got this working for QGIS 2.0. The PyQt setLineEdit method allows a line edit in proximity take ownership over the combo box. Having added "autre" to the combo box, if this item is chosen the combo box turns into a line edit. To solve my issue of data quality control, I added a block against validation if the user does not change the line edit to something other than "autre."

Diametrefield.currentIndexChanged.connect(autre)

def autre():
    global Diam_autrefield, Diametrefield
    if Diametrefield.currentText() == '..Autre':
        QMessageBox.warning(None,'Note de saisie',"Definir 'Autre' Diametre")
        Diametrefield.setLineEdit(Diam_autrefield)

def validate():
    if Diametrefield.currentText() == '..Autre':
        Diametrefield.setStyleSheet("background-color: rgba(255, 107, 107, 150);")
        QMessageBox.warning(None, 'Probleme de saisie',"Veuillez choisir 'Diametre'")
    else:
        myDialog.accept()

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