PDA

View Full Version : Decoding a dragged qListWidget using qDataStream/QByteArray with pyqt



Nachtheim
21st August 2010, 00:39
I sub classed the QScrollArea using the following code:



import sys
from PyQt4 import QtGui, QtCore, Qt

class ScrollArea (QtGui.QScrollArea):
def dragEnterEvent(self, event):
if event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist'):
event.accept()
else:
event.ignore()

def dropEvent(self, event):
mimeData = event.mimeData()
for mimeFormat in mimeData.formats():
if mimeFormat != 'application/x-qabstractitemmodeldatalist':
continue
encoded = mimeData.data(mimeFormat)
#encoded is a QByteArray
print "length:", encoded.length()
print "isNull:", encoded.isNull()
print "isEmpty:", encoded.isEmpty()

stream = QtCore.QDataStream(encoded, QtCore.QIODevice.ReadOnly)
# Returns the index from the qListWidget
index = stream.readUInt32()

In designer I promoted the scrollArea including accepting drops using the class above and set the QListWidget to dragable. The drop is only works when I am dragging onto the promoted scroll area which is what I want.

I can't seem to figure out how to retrieve the item that was dragged out of the qListWidget in the drop event.

I put a bunch or print statments to test of the classes. Length varies on the length of the item, if I have "1" as an item, it prints out:
length: 27
isNull: False
isEmpty: False
status 0
byteOrder 0

"12" shows a length of 29. This goes for the same as letters. Not sure what the number is based off of.

Thanks, this is all I have so far. I did try playing with "stream" and still couldn't get any results.