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")

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

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.

closed as off-topic by Mapperz Feb 16 '15 at 15:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development are off-topic here unless they relate directly to Geographical Information Systems, but they can be asked on Stack Overflow." – Mapperz
If this question can be reworded to fit the rules in the help center, please edit the question.


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."


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

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

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