PyQt: Pulling Abstract Item Data from Mime Data using Drag and Drop.

M

Mudcat

I'm trying to drag/drop info from a TreeWidget into a TextBox. I have
been able to modify the TextEdit box to override the dragEnterEvent
like this:

class TextEdit(QtGui.QTextEdit):
def __init__(self, title, parent):
QtGui.QTextEdit.__init__(self, title, parent)
self.setAcceptDrops(True)

def dragEnterEvent(self, event):
if event.mimeData().hasFormat('text/plain'):
event.accept()
elif (event.mimeData().hasFormat("application/x-
qabstractitemmodeldatalist")):
print event
itemData = event.mimeData().retrieveData("application/x-
qabstractitemmodeldatalist", QtCore.QVariant.List)


The drag is working up until the point I try to actually retrieve the
data. At that point I get an unhandled Runtime Error saying "no access
to protected functions or signals for objects not created in Python".
Obviously I am not retrieving them in the correct format. Does anyone
know how to convert/retrieve the information into a Python list?

Thanks
 
D

David Boddie

The drag is working up until the point I try to actually retrieve the
data. At that point I get an unhandled Runtime Error saying "no access
to protected functions or signals for objects not created in Python".

That's correct, retrieveData() is a protected function in C++ and the
QMimeData object was created by the framework, not you, in this case.
Obviously I am not retrieving them in the correct format. Does anyone
know how to convert/retrieve the information into a Python list?

You could use the data() method to get the serialized data then try to
extract the list item-by-item using the QDataStream class, or perhaps
PyQt has a convenience function to unpack the items.

Alternatively - and this is a bit speculative - perhaps you can copy
the data to another QMimeData object which you have created, and use
its retrieveData() method to retrieve the items.

You might get a more detailed response by asking on the PyQt mailing list:

http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Hope this helps,

David
 
M

Mudcat

That's correct, retrieveData() is a protected function in C++ and the
QMimeData object was created by the framework, not you, in this case.

Ah, well that explains it. Figured as much but was hoping maybe I was
trying to access it incorrectly.

You could use the data() method to get the serialized data then try to
extract the list item-by-item using the QDataStream class, or perhaps
PyQt has a convenience function to unpack the items.

I tried to get info back using data() but kept getting null values.
Same goes for the items() function which is fed with mimeData and is
supposed to return a list. From the documentation it appeared that was
supposed to be the middle-man to re-convert the data. I know the data
existed because I could drag/drop elements from one treeWidget to
another but still couldn't get data back from the target widget.

Alternatively - and this is a bit speculative - perhaps you can copy
the data to another QMimeData object which you have created, and use
its retrieveData() method to retrieve the items.

I started down the path of creating my own MimeType but then realized
it was quicker to just override the drop() function and grab the
selected items from the source widget, just letting the mime data
disappear into the ether. A little klugey but deadline is fast
approaching. However I'll give the pyqt mailing list a shot. I didn't
know there was one just for pyqt.

Thanks for all the help,
Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top