Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, August 2006
QComboBox.itemData() returns invalid QVariant


Message 1 in thread

Hi,

I am using PyQt4 and Qt 4.1.4. When I call QComboBox.itemData(), it always 
returns an invalid QVariant. For example:

id = 1
self.addItem(rev, QVariant(id))
self.itemData(0).toInt() --> 0, False
self.itemData(0).isValid() --> False

I searched the list archive and see any hits with the same issue. I must be 
doing something wrong. Any ideas?

Thanks,
Tom

--
 [ signature omitted ] 

Message 2 in thread

On 30.08.06 10:47:48, Tom Brown wrote:
> I am using PyQt4 and Qt 4.1.4. When I call QComboBox.itemData(), it always 
> returns an invalid QVariant. For example:

> id = 1
> self.addItem(rev, QVariant(id))
> self.itemData(0).toInt() --> 0, False
> self.itemData(0).isValid() --> False

Can't reproduce this. Are you sure that the addItem call is the first to
add something to the box? Because it appends the new item and thus 0
might not be the last item (i.e. the one you just added). This works
here:

>>> from PyQt4 import QtCore, QtGui
>>> import sys
>>> app=QtGui.QApplication(sys.argv)
>>> w=QtGui.QComboBox()
>>> w.addItem("foo",QtCore.QVariant(9))
>>> w.itemData(0)
<PyQt4.QtCore.QVariant object at 0xb7c68e6c>
>>> w.itemData(0).isValid()
True
>>> w.itemData(0).toInt()
(9, True)

Also make sure rev is a QString and not None, if it is PyQt4 might
choose the 2nd incarnation of addItem:

void QComboBox::addItem ( const QIcon & icon, const QString & text, const QVariant & userData = QVariant() )

i.e. you get a new item with no icon and the text "1", but no custom
data either.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

On Wednesday 30 August 2006 12:28, Andreas Pakulat wrote:
> On 30.08.06 10:47:48, Tom Brown wrote:
> > I am using PyQt4 and Qt 4.1.4. When I call QComboBox.itemData(), it
> > always returns an invalid QVariant. For example:
> >
> > id = 1
> > self.addItem(rev, QVariant(id))
> > self.itemData(0).toInt() --> 0, False
> > self.itemData(0).isValid() --> False
>
> Can't reproduce this. Are you sure that the addItem call is the first to
> add something to the box? Because it appends the new item and thus 0
> might not be the last item (i.e. the one you just added). This works
>
> here:
> >>> from PyQt4 import QtCore, QtGui
> >>> import sys
> >>> app=QtGui.QApplication(sys.argv)
> >>> w=QtGui.QComboBox()
> >>> w.addItem("foo",QtCore.QVariant(9))
> >>> w.itemData(0)
>
> <PyQt4.QtCore.QVariant object at 0xb7c68e6c>
>
> >>> w.itemData(0).isValid()
>
> True
>
> >>> w.itemData(0).toInt()
>
> (9, True)
>
> Also make sure rev is a QString and not None, if it is PyQt4 might
> choose the 2nd incarnation of addItem:
>
> void QComboBox::addItem ( const QIcon & icon, const QString & text, const
> QVariant & userData = QVariant() )
>
> i.e. you get a new item with no icon and the text "1", but no custom
> data either.
>
> Andreas

I tried your test interactively and it worked. However, I still can't get my 
combobox in my application to work. I modified my function in my subclassed 
QComboBox that adds items to the combobox to print info about the data added 
with the text. Each call to itemData() returns an invalid QVariant as shown 
below. The rev values show up in the drop down list as expected.

Thanks,
Tom

  def setRevs(self, revs):
    print 'setRevs'
    for rev, revID in revs:
      print ' ', rev, revID 
      if (not rev) or (not revID):
        continue
      var = QVariant(revID)
      print '======================='
      print 'var ------>', var
      print 'id ------->', id(var)
      print 'isNull --->', var.isNull()
      print 'isValid -->', var.isValid()
      print 'toInt ---->', var.toInt()
      print
      self.addItem(rev, var)
      i = self.count() - 1
      var = self.itemData(i)
      print 'var ------>', var
      print 'id ------->', id(var)
      print 'isNull --->', var.isNull()
      print 'isValid -->', var.isValid()
      print 'toInt ---->', var.toInt()
      print '======================='
      print
    for i in xrange(0, self.count()):
      var = self.itemData(i)
      print '======================='
      print 'i -------->', i
      print 'var ------>', var
      print 'id ------->', id(var)
      print 'isNull --->', var.isNull()
      print 'isValid -->', var.isValid()
      print 'toInt ---->', var.toInt()
      print '======================='
      print

setRevs
  0 1
=======================
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> False
isValid --> True
toInt ----> (1, True)

cpuRevChanged
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

  1 2
=======================
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> False
isValid --> True
toInt ----> (2, True)

var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

  2 3
=======================
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> False
isValid --> True
toInt ----> (3, True)

var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

  3 4
=======================
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> False
isValid --> True
toInt ----> (4, True)

var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

  4 5
=======================
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> False
isValid --> True
toInt ----> (5, True)

var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

  5 6
=======================
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> False
isValid --> True
toInt ----> (6, True)

var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

  5A 7
=======================
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> False
isValid --> True
toInt ----> (7, True)

var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

=======================
i --------> 0
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

=======================
i --------> 1
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

=======================
i --------> 2
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

=======================
i --------> 3
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

=======================
i --------> 4
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

=======================
i --------> 5
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bec>
id -------> -1231840276
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

=======================
i --------> 6
var ------> <PyQt4.QtCore.QVariant object at 0xb6939bac>
id -------> -1231840340
isNull ---> True
isValid --> False
toInt ----> (0, False)
=======================

--
 [ signature omitted ] 

Message 4 in thread

On 30.08.06 14:19:07, Tom Brown wrote:
> I tried your test interactively and it worked. However, I still can't get my 
> combobox in my application to work. I modified my function in my subclassed 
> QComboBox that adds items to the combobox to print info about the data added 
> with the text. Each call to itemData() returns an invalid QVariant as shown 
> below. The rev values show up in the drop down list as expected.

Try to reduce your application to a minimal example and provide the full
source, here or on the PyQt mailinglist.

Andreas

-- 
 [ signature omitted ]