[PyQt] Trouble reading a model index from an "untouched" combo box
felix.claudio at gmail.com
Wed Jan 27 19:33:18 GMT 2010
> 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.
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.
For this part about getting ITEM_ID, that's the purpose of the
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()
It retrieves the index corresponding to the ITEM_NAME column in the
combo box, but then I query the table model for the same row but
specifying the ITEM_ID index, so I really get the right one from the
table, not tem combo box.
I really think there might be a better way though... the whole purpose
of this dialog is to set restrictions on a SQL Query based on the
foreign keys found in the main table (each combo box should show a
text field associated to a foreign key value), so maybe I can replace
the QSqlQuery by a QSqlRelationalTableModel altogether with each
combo box showing the named field set through an QSqlRelation. I know
that works fine, but if I change a combo box item I suspect it will
modify the key value on the main table, and this is not what I want.
In fact I just want to filter the results and sum them up in SQL,
something like this (consider this table):
Volume (float) Cost (float) Supplier_ID (int) Period_ID (int)
The query would sum up all the volume and cost records of some product
if no Combo box were enabled, but if supplier was enabled it would sum
up just the volume and cost for products from a given supplier or if
period combo box were enabled also, just the given periods for that
given supplier would be considered. Something like "select
sum(Volume), sum(Cost) from table where SUPPLIER_ID = x and PERIOD_ID
= y" (the where clause depends on the combos being enabled or not).
Thank all you guys for your help!
More information about the PyQt