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

Andreas Pakulat apaku at gmx.de
Wed Jan 27 19:53:32 GMT 2010

On 27.01.10 17:33:18, Claudio Felix wrote:
> > Well, the combobox has initially no selected/current entry - AFAIK. Only
> > once you select one it'll be set. Hence the invalid index.
> >
> > However a setCurrentIndex with a value != 0 on the combobox should
> > change that already I think.
> >
> > Another thing in your case is that box.view().currentIndex() will not be
> > an index that contains the ITEM_ID value, because the current index will
> > be pointing to the name-cell. Each index indvidually refers to a single
> > cell and you've used the ITEM_NAME column for the combobox, so its view
> > will only give you indexes from that column.
> >
> > Andreas
> >
> Andreas,
> One of Demetrius' previous suggestions was doing that indeed (setting
> currentIndex), and I tried it both for zero and non-zero indexes, but
> to no avail. The combo box starts showing the right item but the model
> index still comes invalid. If I just click, voila, I get a valid
> index. Really strange.

Looking at the source code in Qt, I think the reason for this is because
there's no connection between the currentIndex() in the itemview and the
one in its selectionModel. In particular the currentChanged() slot in
the itemview doesn't update its internal currentIndex. I think thats a
bug in Qt.

So you'll have to ignore the view here and work with the row from the
combobox to generate a model index. This should work:

def getItemID(self:
    idx = self.model.index( self.comboBox.currentIndex(), self.model.fieldIndex("ITEM_ID"), QModelIndex() )
    return self.model.data(idx).toInt()[0]


You are a bundle of energy, always on the go.

More information about the PyQt mailing list