[PyQt] Trouble reading a model index from an "untouched" combo box

Claudio Felix felix.claudio at gmail.com
Tue Jan 26 15:34:11 GMT 2010


Hi everyone,

I'm developing an app which loads data from a database table into a
QSqlTableModel. At some point I associate this model to a Combo Box
(using QComboBox.setModel). The strange thing is, when I try to read
the current model index from the combo box view, for getting the
selected item's data from another database field on the same model
record, I get a invalid index, but If I just click on the combo box (I
don't even have to change the item) before trying to read its index,
it returns a valid model index. So, it is really necessary to do
something before, like generating a "currentIndexChanged" signal? What
I did was like this:


   def __init__(self):
        self.model = QSqlTableModel()
        self.model.setTable("dbtable")
        self.model.select()
        self.comboBox = QComboBox()
        self.comboBox.setModel(self.model)
        self.comboBox.setModelColumn("ITEM_NAME")


   def getItemID(self):
        index = self.comboBox.view().currentIndex()
        if not index.isValid():
            raise ValueError, "invalid index"                   # Here
I always get the error if I don't click on the combo box before
        row = index.row()
        column = self.model.fieldIndex("ITEM_ID")
        return self.model.data(self.model.index(row, column)).toInt()[0]

Thanks!


More information about the PyQt mailing list