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

Demetrius Cassidy dcassidy36 at mass.rr.com
Tue Jan 26 21:59:08 GMT 2010


How about selecting index 0 once the combobox is initialized with the 
database data? It sounds to me that it has no valid index when first 
initialized, and if you try to programmatically select the first index, 
it's returning an invalid one. You don't need to generate a signal, just 
use something like .setCurrentIndex(0).

Claudio Felix wrote:
> 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!
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
>   



More information about the PyQt mailing list