1

If Qt.UserRole the model's headerData() returns a Python list variable:

if role==Qt.UserRole:
    return QVariant(['one','two','three'])

Instead of a regular Python list a function that calls with:

returnedValue = myModel(index.column(), Qt.Horizontal, Qt.UserRole)

gets a QVariant object:

<PyQt4.QtCore.QVariant object at 0x10d3b67c0>

An attempt to convert the returned QVariant object to Python using:

pythonList=returnedValue.toPyObject()

didn't work. I have tried to do:

for each in returnedValue.toList(): print each

But that still prints out some QVariants. What method should be used to convert QVariant to Python list?

alphanumeric
  • 17,967
  • 64
  • 244
  • 392

1 Answers1

4

QVariant is a general container for almost all kinds of built-in types, so in order to cast QVariant data back, you need to know what kind of data it is stored.

from PyQt4.QtCore import QVariant

a = QVariant(['one', 'two', 'three'])

aList = [unicode(i.toString()) for i in a.toList()]
print aList

output is as :

[u'one', u'two', u'three']

Nejat
  • 31,784
  • 12
  • 106
  • 138
Cui Heng
  • 1,265
  • 8
  • 10