Trying to implement drag and drop for QAbstractItemModel

Rodrigo de Salvo Braz rodrigobraz at
Tue Apr 20 19:34:34 BST 2021

Thank you Maurizio, I think you have understood my question.

So it seems that QMimeData works a bit like a "dict", associating MIME
types to QByteArrays of data, and can therefore hold multiple types of data
at the same time. That's what wasn't clear to me.

Thank you again!


On Tue, Apr 20, 2021 at 4:10 AM Maurizio Berti <maurizio.berti at>

> You seem to be talking about the possibility of *changing* the chosen
>> format to a different one but still supporting a single format, but I am
>> talking about mimeTypes() returning a list with *more* than one type,
>> making the model have to support multiple formats at the same time. It's
>> not clear to me how to do that in mimeData(), dropMimeData() and QMimeData.
>> In that scenario, it sounds like mimeData() would have to produce MIME
>> data in multiple formats, and dropMimeData would have to support data in
>> multiple formats. For one thing, it's not clear how to check the format of
>> a QMimeData object. For another, it isn't clear how mimeData() can return a
>> single QMimeData while supporting multiple formats.
> I'm not sure I'm understanding your question.
> QMimeData is a container of data, stored in multiple fields identified by
> their "format", the assumption is that a format identifier always has the
> same data format specification (so, x-qabstractitemmodeldatalist should
> always have the structure described above, but nobody stops you to do
> anything else).
> Let's suppose you have a list model with a series of urls and number of
> visits, and you want to allow both internal d&d, but also custom data
> format that includes the list as url/visits pairs, and standard OS support
> for those URLs:
>     def mimeData(self, indexes):
>         mimeData = super().mimeData(indexes)
>         urlList = []
>         ba = QByteArray()
>         stream = QDataStream(ba, QIODevice.WriteOnly)
>         for index in indexes:
>             url, visits = self.urls[index.row()]
>             urlList.append(QtCore.QUrl(url))
>             stream.writeQString(url)
>             stream.writeInt(visits)
>         mimeData.setUrls(urlList)
>         mimeData.setData('myApp/visitedUrlList', ba)
>         return mimeData
> In this case, we have the current base implementation of mimeData (which
> is compliant with the Qt model d&d features), the url format used for
> browsers, *and* another custom "mime format"  that we could use in this
> model or any widget/model that could be interested in such a format.
> class SomeWidget(QWidget):
>     # ...
>     def dropEvent(self, event):
>         if event.mimeData().hasFormat('myApp/visitedUrlList'):
>             stream =
> QDataStream(event.mimeData().data('myApp/visitedUrlList'),
> QIODevice.ReadOnly)
>             while not stream.atEnd():
>                 url = stream.readQString()
>                 visits = stream.readInt()
>                 # ...
> I hope I've understood what you were asking.
> Maurizio
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the PyQt mailing list