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

Andreas Pakulat apaku at gmx.de
Wed Jan 27 18:49:54 GMT 2010

On 27.01.10 09:08:13, Claudio Felix wrote:
> 2010/1/27 Andreas Pakulat <apaku at gmx.de>:
> > On 26.01.10 17:41:40, Demetrius Cassidy wrote:
> >> I don't think you need to use the view pointer at all - it's
> >> returning QAbstractItemView *, which I would assume it would need to
> >> be casted to the proper class in C++. If that's so, by design the
> >> Abstract class will return an invalid index.
> >
> > I don't think so ;) The function currentIndex on the view is not
> > virtual, so its implemented in the abstract class already properly.
> >
> > And to get a model index to index into the model its easier to ask the
> > view for it, rather than constructing it yourself with model->index(
> > combobox->currentIndex(), <model-column>, QModelIndex())
> >
> > Andreas
> >
> Andreas,
> That was the way I was thinking when I tried the code in the beggining
> of this thread.. but I'm still stuck retrieving an invalid index if I
> don't at least click on the combo box (if I do click on it I get a
> valid index just fine).
> If you take a look at the code, my intent was to get the ITEM_ID
> field, which is the table's primary key which corresponds to the item
> selected on the combo box. Since I have the model for the table
> already, I found natural to use it for displaying the ITEM_NAME field
> on the combo box, so I would get the selected item model index from
> its view to get the ITEM_ID from the table model and use it to
> optionally filter data on a subsequent SQL Query. I'm intrigued by
> this QComboBox behavior, for the model was given a "select()" before
> being associated to it, so I think it should return a valid index.

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.


You plan things that you do not even attempt because of your extreme caution.

More information about the PyQt mailing list