[PyQt] QAbstractListModel.setData gets passed a QVariant that cannot be converted to user data type
Phil Thompson
phil at riverbankcomputing.com
Sat Jun 4 15:52:56 BST 2011
On Fri, 03 Jun 2011 22:00:42 +0200, Thorben Kroeger
<thorben.kroeger at iwr.uni-heidelberg.de> wrote:
> 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...
It's because StarRating can't be pickled because of the starPolygon and
diamondPolygon attributes. To fix it make them class attributes rather than
instance attributes (which they should be anyway).
Phil
More information about the PyQt
mailing list