[PyQt] dense about qvariant and dict

Phil Thompson phil at riverbankcomputing.com
Sat Dec 8 09:33:30 GMT 2012

On Fri, 7 Dec 2012 16:06:28 -0800, David Cortesi <davecortesi at gmail.com>
> OK, I implement QListView.itemData() returning a python dict,
> which then arrives at QListView.setItemData() as a QVariant
> of length 1. The problem is, how to cast that qv back to a dict?
> I have read
> http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qvariant.html
> and noted especially the .convert method, but qv.convert(dict) produces
> the amusingly self-referential error,
> TypeError: QVariant.convert(Type): argument 1 has unexpected type 'type'
> dict(qv) doesn't do it. qv.toHash produces an empty dict.
> qv.toMap produces a QMap with the original keys as QStrings and
> the original data as QVariants. OK, but I'd rather have the python
> dict -- is there a way to get it, or do I have to work with the QMap

If PyQt can convert the dict to a QVariantMap then it will do, and discard
the original dict. It will only preserve the Python object if it can't do
anything else with it.


More information about the PyQt mailing list