[PyQt] Pulling Abstract Item Data out of MimeData from Drag and Drop

David Boddie david at boddie.org.uk
Sun Dec 14 21:45:46 GMT 2008


On Sun Dec 14 19:02:30 GMT 2008, Marc Nations wrote:

> I am using PyQt4 (Qt 4.4.3 with Python 2.6) on Windows XP. I am trying to
> drag treeWidget info from one widget to another. The target widget will
> likely be a modified tree widget as I need to combine a tree with a table
> to get the proper data mix I need.

[...]

> So then I tried to pull serialized data out using the data() function which
> returned an empty set.

The main problem would appear to be that the data is delivered in what is
basically an internal format: the application/x-qstandarditemmodeldatalist
MIME type. Qt uses this internally to allow drag and drop to occur between
views, but it's probably better to handle encoding and decoding of data
yourself.

> Then I tried to pull data out using the items() 
> function passing in the mimeData object. Here is an example that I used to
> try and get this working with another widget type.
>
> > class ListBoxDnD(QListWidget):
> > def __init__(self, parent):
> > QListWidget.__init__(self, parent)
> > self.setDragEnabled(True)
> > self.setAcceptDrops(True)
> > ...
> >
> > def dropEvent(self, e):
> > print self.items(e.mimeData())
>
> This also returned an empty set. I thought if anything should translate the
> data for me, this should do it. I know the data is there because I can see
> the drag and drop working as data items are being correctly added to the
> target widget. However the same mimeData is returning no data when I try to
> access it directly. I can't figure that out.

It may be related to the internal format being used.

> I was able to find a work-around by overriding the drop function and just
> grabbing the selected items from the source widget directly. This actually
> works a little better than the default drag/drop behavior because I need
> the selected tree items plus all the child items with a little extra data
> pulled from a database. However at some point I may need to use the
> MimeData functionality as it was originally intended, so hopefully someone
> will know how to get this to work correctly.

Getting drag and drop to work with item views is usually a matter of setting
everything up in a certain way, so it can be a frustrating experience. The
information at

  http://doc.trolltech.com/4.4/model-view-dnd.html

tries to describe how to set it up in some basic situations, but it assumes
that you want to add drag and drop support to your own model, and you may
wish to use a standard model instead.

It's been a while since I seriously looked at item views but I hope this
helps you figure it all out.

David


More information about the PyQt mailing list