[PyQt] QAbstractListModel.setData gets passed a QVariant that cannot be converted to user data type

Thorben Kroeger thorben.kroeger at iwr.uni-heidelberg.de
Fri Jun 3 21:00:42 BST 2011


Hello everyone,

I'm implementing a QAbstractListModel derived class in PyQt that uses a 
custom data type.

However, in QAbstractListModel.setData(index, value, role = 
Qt.EditRole), I get passed value as a QVariant, and I cannot convert it 
to my custom type:

value.toPyObject()

yields: "TypeError: unable to convert a QVariant back to a Python object"

Please find the short code (<300 lines, adpated from the StarDelegate 
example of Qt) attached.

The problem occurs when using drag/drop to reorder the items inside the 
view. You'll see the above error printed, and the dropped item is 
replaced by a new instance of StarRating, which has 5 stars by default.

(Another problem I have with this example is that dragging items on top 
of empty space in the QListView deletes the item, though I specified 
Qt.MoveOnly in supportedDropActions... Maybe you happen to know what the 
problem is here as well?)

How can I fix this?

Thorben

P.S.: I posted this to stackoverflow 
(http://stackoverflow.com/questions/6226830/pyqt-qabstractlistmodel-setdata-gets-passed-a-qvariant-that-cannot-be-converted) 
but believe this list is a better place for that question...

-------------- next part --------------
A non-text attachment was scrubbed...
Name: modelview_qvariant_problem.py
Type: text/x-python
Size: 9863 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110603/bb059123/attachment.py>


More information about the PyQt mailing list