1

I am trying to create a QGIS custom form which allows users to select a value (name) among others in a comboBox, and saves said value with its corresponding code (taken from another table in the database) in my database. It doesn't seem that PyQt allows for direct link between a comboBox and a database (see here for example), so I copy the comboBox value in a QLineEdit in order to save it in the database. It seems to work fine as long as I'm in editing mode (changing the value in the comboBox changes the corresponding code), but when I get out of editing mode and save I get the following error in the console:

AttributeError: 'list' object has no attribute 'setText'

The last value I choose before I get the error and its code are saved, but afterwards I'm unable to change them again using my form - even if I go back into editing mode. What puzzles me the most is that I already created another form using the same logic, and it works perfectly! I don't understand why I get this error and how to solve it.

Here 's my (not working) code :

def formExploitationsOpen(dialog, layerid, featureid):

global myDialog
myDialog = dialog

global buttonBox
buttonBox = dialog.findChild(QDialogButtonBox, "buttonBox")

global groupBox
groupBox = dialog.findChild(QGroupBox, "groupBox")

global commentaire
commentaire = dialog.findChild(QPlainTextEdit, "commentaire")

global code_pae
code_pae = dialog.findChild(QLineEdit, "code_pae")

global combo_nom_pae
combo_nom_pae = dialog.findChild(QComboBox, "combo_nom_pae")

global nom_pae
nom_pae = dialog.findChild(QLineEdit, "nom_pae")
nom_pae.setVisible(0)

def change_nom_pae():
    text_pae = combo_nom_pae.currentText()
    nom_pae.setText(text_pae)
    SQL_nom_pae = "SELECT nom_pae FROM ref.pae_mae"
    cur_qgis.execute(SQL_nom_pae)
    n = combo_nom_pae.currentIndex()
    text_pae2 = list(itertools.chain.from_iterable(cur_qgis))[n]
    SQL_code_pae = "SELECT code_pae FROM ref.pae_mae WHERE nom_pae ILIKE '" + text_pae2 + "'"
    cur_qgis.execute(SQL_code_pae)
    current_code = list(itertools.chain.from_iterable(cur_qgis))[0]
    code_pae.setText(current_code)

QObject.connect(combo_nom_pae, SIGNAL("currentIndexChanged(int)"), change_nom_pae)

currentLayer = qgis.utils.iface.activeLayer()  

if currentLayer.isEditable() == True:
    code_pae.setEnabled(False)

if nom_pae.text() != "" and nom_pae.text() != "NULL" and nom_pae.text() != "'NA'::text":
    valeur = nom_pae.text()
    SQL_nom_pae = "SELECT nom_pae FROM ref.pae_mae WHERE ref.pae_mae.nom_pae ILIKE '" + valeur + "'"
    cur_qgis.execute(SQL_nom_pae)
    nom_pae = cur_qgis.fetchall()
    nom_pae_value = nom_pae[0][0]
else:
    nom_pae_value = 'NA'

SQL_noms_pae = "SELECT nom_pae FROM ref.pae_mae"
cur_qgis.execute(SQL_noms_pae)
noms_pae = cur_qgis.fetchall()

combo_nom_pae.clear()
i = 0
j = -1
while i < len(noms_pae):
    SQL_noms_pae = "SELECT nom_pae FROM ref.pae_mae"
    cur_qgis.execute(SQL_noms_pae)
    paeListe = list(itertools.chain.from_iterable(cur_qgis))[i]
    combo_nom_pae.addItems(noms_pae[i])
    if nom_pae_value == paeListe:
        j = i
    i = i + 1
combo_nom_pae.setCurrentIndex(j)

and here is the (working) code from the other form, as a reference :

def formPiezoOpen(dialog, layerid, featureid):

global myDialog
myDialog = dialog

global buttonBox
buttonBox = dialog.findChild(QDialogButtonBox, "buttonBox")

global groupBox
groupBox = dialog.findChild(QGroupBox, "groupBox")

global commentaire
commentaire = dialog.findChild(QPlainTextEdit, "commentaire")

global nom_sonde
nom_sonde = dialog.findChild(QLineEdit, "nom_sonde")

global comboNumSerie
comboNumSerie = dialog.findChild(QComboBox, "comboNumSerie")

global num_serie
num_serie = dialog.findChild(QLineEdit, "num_serie")
num_serie.setVisible(0)

def change_sonde():
    text_numserie = comboNumSerie.currentText()
    num_serie.setText(text_numserie)
    SQL_num_serie = "SELECT num_serie FROM ref.infos_piezo"
    cur_qgis.execute(SQL_num_serie)
    n = comboNumSerie.currentIndex()
    text_numserie2 = list(itertools.chain.from_iterable(cur_qgis))[n]
    SQL_nom_sonde = "SELECT nom_sonde FROM ref.infos_piezo WHERE num_serie ILIKE('" + text_numserie2 + "')"
    cur_qgis.execute(SQL_nom_sonde)
    current_name = list(itertools.chain.from_iterable(cur_qgis))[0]
    nom_sonde.setText(current_name)

QObject.connect(comboNumSerie, SIGNAL("currentIndexChanged(int)"), change_sonde)

currentLayer = qgis.utils.iface.activeLayer()

if currentLayer.isEditable() == True:
    nom_sonde.setEnabled(False)

if num_serie.text() != "" and num_serie.text() != "NULL" and num_serie.text() != "'NA'::text":
    valeur = num_serie.text()
    SQL_numero = "SELECT num_serie FROM ref.infos_piezo WHERE ref.infos_piezo.num_serie ILIKE '" + valeur + "'"
    cur_qgis.execute(SQL_numero)
    numero = cur_qgis.fetchall()
    numserie_value = numero[0][0]
else:
    numserie_value = 'NA'

SQL_numeros = "SELECT num_serie FROM ref.infos_piezo"
cur_qgis.execute(SQL_numeros)
numsserie = cur_qgis.fetchall()

comboNumSerie.clear()
i = 0
j = -1
while i < len(numsserie):
    SQL_numeros = "SELECT num_serie FROM ref.infos_piezo"
    cur_qgis.execute(SQL_numeros)
    numerosListe = list(itertools.chain.from_iterable(cur_qgis))[i]
    comboNumSerie.addItems(numsserie[i])
    if numserie_value == numerosListe:
        j = i
    i = i + 1
comboNumSerie.setCurrentIndex(j)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.