1

I'm converting a mimeData object to a human-readable string when it gets dropped onto another widget. That's all working fine, with the exception of a slight spacing issue.

Here's the code:

import re
event.accept()
format = "application/x-qabstractitemmodeldatalist"
data = event.mimeData().data(format)
pattern = re.compile('\W')
item = re.sub(pattern, '', str(data))
print str(data) # prints smiley face, other unicode characters, then J  o  h  n  n  y   D  e  p p
print item # prints JohnnyDepp

How can I get Johnny Depp out of the mimeData instead of JohnnyDepp or J o h n n y D e p p?

jpyams
  • 4,030
  • 9
  • 41
  • 66
  • I was hoping that someone would respond with a more elegant way to extract the text from the mimeData- perhaps something I overlooked or misunderstood in the documentation- or a more clever way to handle the regular expression to extract all the spaces except one in the middle of the actual text. I should clarify that the beginning of the str(data) has many spaces and various unicode characters and that the spaces between the alpha characters are not necessarily consistent. Perhaps regular expressions are not my strong point, but I'm unaware of how to cleanly solve that problem. –  Dec 22 '11 at 21:27

3 Answers3

0

I guess you could use something like this:

>>> re.sub('\W+', ' ', 'some space   here')
'some space here'

It would replace every chunk of whitespace with a single space.

Also see here: A simple way to remove multiple spaces in a string in Python

Community
  • 1
  • 1
Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
0

It looks like double spaces are being placed between every character. Note the triple space between the 'y' and the 'D'.

>>> line = "J  o  h  n  n  y   D  e  p  p"
>>> re.sub('\W+', '', line)
'JohnnyDepp'
>>> re.sub('\W+', ' ', line)
'J o h n n y D e p p'
>>> re.sub('\W+', ' ', line.replace('  ', ''))
'Johnny Depp'

I think the last one should return what you are looking for by removing the double spaces first.

sgallen
  • 2,079
  • 13
  • 10
0

The mime data is a QByteArray that contains a serialized representation of the dropped item.

So you will need a function to decode the data, like this:

def decodeMimeData(self, data):
    result = {}
    value = QtCore.QVariant()
    stream = QtCore.QDataStream(data)
    while not stream.atEnd():
        row = stream.readInt32()
        col = stream.readInt32()
        item = result.setdefault(col, {})
        for role in range(stream.readInt32()):
            key = QtCore.Qt.ItemDataRole(stream.readInt32())
            stream >> value
            item[key] = value.toPyObject()
    return result

The result is in the form of a dict containing dicts for each column of the dropped item. The column dicts contain data for each of the roles represented.

So to get the text from column zero, you would do:

format = 'application/x-qabstractitemmodeldatalist'
data = event.mimeData().data(format)
item = self.decodeMimeData(data)
print item[0][QtCore.Qt.DisplayRole]
ekhumoro
  • 115,249
  • 20
  • 229
  • 336