Retrieve id in comboBox

1

I have the following function to fill a comboBox with the data entered in the form by the user, this works correctly.

def onTextChanged(self, text):
    if text:
  sql = ("SELECT sec.Nombre as Seccion,sec,idSeccion FROM [Lote] lot \
            inner join [Seccion] sec on sec.idInforme = lot.idInforme \
                Where idLote = '%s'") % \
              (self.dlg.lote.text())
        cursorLista.execute(sql)
        self.dlg.comboBox.clear()
        self.dlg.comboBox.addItems([str(x[0]) for x in cursorLista])

Then I'll call from

self.dlg.lote.textChanged.connect(self.onTextChanged)
result = self.dlg.exec_()

The problem is that now I need to get the id of the section that was selected in the comboBox and save it in a variable, as I can get it since in the query I am bringing all the idSeccion for that Batch.

    
asked by Sebastian 06.08.2018 в 12:55
source

1 answer

2

QComboBox allows you to store additional information associated to each item using setItemData() , in your case you could do the idSection, and recover it using itemData() :

def onTextChanged(self, text):
    if text:
        sql = ("SELECT sec.Nombre as Seccion,sec,idSeccion FROM [Lote] lot \
            inner join [Seccion] sec on sec.idInforme = lot.idInforme \
                Where idLote = '%s'") % \
              (self.dlg.lote.text())
        cursorLista.execute(sql)
        self.dlg.comboBox.clear()
        for i, x in enumerate(cursorLista):
            self.dlg.comboBox.addItem(str(x[0]))
            self.dlg.comboBox.setItemData(i, x[1])

    ...
    result = self.dlg.exec_()
    if result:
        ix = self.dlg.comboBox.currentIndex()
        idSection = self.dlg.comboBox.itemData(ix)
    
answered by 06.08.2018 / 13:09
source