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

2010-01-30 Thread Claudio Felix

 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:
    self.comboBox.setCurrentIndex(0)
    idx = self.model.index( self.comboBox.currentIndex(), 
 self.model.fieldIndex(ITEM_ID), QModelIndex() )
    return self.model.data(idx).toInt()[0]

 Andreas


Thanks Andreas,

It worked fine that way. Since these DB records won't change anyway, I
think indexing the ITEM_ID field by the combo box item index will do
the job in the long run for this particular project. About this
potential bug of the lack of connection between the currentIndex in
the itemview and the one in the selectionModel, should we report this
to Phil or maybe open a bug report for QT? I'm not familiar with QT's
source code, but if that is really a bug (and it looks like it is) it
would be nice to get it fixed, huh?

Thank you for your help!

Claudio
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-30 Thread Andreas Pakulat
On 30.01.10 22:44:24, Claudio Felix wrote:
 
  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:
     self.comboBox.setCurrentIndex(0)
     idx = self.model.index( self.comboBox.currentIndex(), 
  self.model.fieldIndex(ITEM_ID), QModelIndex() )
     return self.model.data(idx).toInt()[0]
 
  Andreas
 
 It worked fine that way. Since these DB records won't change anyway, I
 think indexing the ITEM_ID field by the combo box item index will do
 the job in the long run for this particular project. About this
 potential bug of the lack of connection between the currentIndex in
 the itemview and the one in the selectionModel, should we report this
 to Phil or maybe open a bug report for QT? I'm not familiar with QT's
 source code, but if that is really a bug (and it looks like it is) it
 would be nice to get it fixed, huh?

File it with Qt as bug, for getting their attention it should be enough
if you can provide a small example demonstrating the problem (IIRC they
prefer C++).

Andreas
 
-- 
You will be run over by a bus.
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-27 Thread Demetrius Cassidy
Just took a peak at QAbstractItemView class and your right it's fully 
implemented. The view is asking the selectionModel what the current 
selected index is, and if it has no selection model (not sure if that's 
even possible) it returns an invalid QModelIndex. QComboBox.currentIndex 
is just asking the QSqlTableModel for the row. It doesn't hurt to make a 
1 line code change to test it out. I use QComboBox.currentIndex in my 
app, though I have no need to change the model in QComboBox.


Andreas Pakulat wrote:

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

  


___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

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

2010-01-27 Thread Claudio Felix
2010/1/27 Andreas Pakulat ap...@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.


Claudio
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-27 Thread Andreas Pakulat
On 27.01.10 09:08:13, Claudio Felix wrote:
 2010/1/27 Andreas Pakulat ap...@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.

Andreas

-- 
You plan things that you do not even attempt because of your extreme caution.
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-27 Thread Claudio Felix
 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.

For this part about getting ITEM_ID, that's the purpose of the
getItemID function:

  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]

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!

Claudio
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-27 Thread Andreas Pakulat
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:
self.comboBox.setCurrentIndex(0)
idx = self.model.index( self.comboBox.currentIndex(), 
self.model.fieldIndex(ITEM_ID), QModelIndex() )
return self.model.data(idx).toInt()[0]

Andreas

-- 
You are a bundle of energy, always on the go.
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-26 Thread Demetrius Cassidy
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 listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

  


___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-26 Thread Claudio Felix
2010/1/26 Demetrius Cassidy dcassid...@mass.rr.com:
 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).



Thanks for the answer Demetrius. Unfortunately, it didn't change it.
Maybe I do have to use a QDataWidgetMapper on the combo box? I thought
just getting the model associated with it should do it.. it shows all
the models' values indeed...

Cheers...
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-26 Thread Demetrius Cassidy
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. Try to use self.comboBox.currentIndex() instead 
- it returns -1 if theres no selected index (which there won't be when 
you first set the model until you manually or programatically select one)


http://doc.trolltech.com/4.6/qcombobox.html#currentIndex-prop

Claudio Felix wrote:

2010/1/26 Demetrius Cassidy dcassid...@mass.rr.com:
  

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).





Thanks for the answer Demetrius. Unfortunately, it didn't change it.
Maybe I do have to use a QDataWidgetMapper on the combo box? I thought
just getting the model associated with it should do it.. it shows all
the models' values indeed...

Cheers...
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

  


___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


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

2010-01-26 Thread Andreas Pakulat
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

-- 
You dialed 5483.
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt