Trying to implement drag and drop for QAbstractItemModel

Rodrigo de Salvo Braz rodrigobraz at gmail.com
Tue Apr 20 11:01:33 BST 2021


Thanks again, Maurizio, good stuff!

On Tue, Apr 20, 2021 at 2:34 AM Maurizio Berti <maurizio.berti at gmail.com>
wrote:

>
> As far as I know, it's not clearly specified anywhere (there's a generic
> description here:
> https://doc.qt.io/qt-5/model-view-programming.html#mime-data ), but you
> can find it in the sources of QAbstractItemModel:mimeData, which creates a
> QDataStream with data encoded using :itemData() in the form:
>
> stream << row << column << itemData(index)
>
> (note that the above syntax is also available on PyQt, while unfortunately
> it's not possible with the opposite >> assignment, due to Python dynamic
> typing)
>

I see. I've seen a suggestion on SO that I thought was interesting. Instead
of learning how to decode this format, one can write

  model = QStandardItemModel()
  model.dropMimeData(event.mimeData(), Qt.CopyAction, 0,0, QModelIndex())

and then simply read the items from this model. Not that useful now that
you told me the format, anyway...

Going a bit beyond this particular example, what happens if the method
>> mimeData has a list with multiple types? Method mimeData is supposed to
>> return a single QMimeData, so there will be a single encoding of the data,
>> and if so, for which of the multiple mime types?
>>
>
> In theory, you should always ensure that the format is
> application/x-qabstractitemmodeldatalist before going on with this
> implementation; according to your needs, you could always implement your
> own alternatives for other types of mime data.
>
> Also, in case of multiple supported mime types, how can dropMimeData know
>> the format in which 'data' is encoded? It does not seem QMimeData provides
>> a method indicating its format.
>>
>
> By default there is only one supported mime type
> (x-qabstractitemmodeldatalist) for models, but QMimeData can theoretically
> support any serializable content set using setData(), and you can add that
> support by implementing mimeData() (just override it, call the base
> implementation and then add more data formats): for instance,
> QStandardItemModel also
> creates application/x-qstandarditemmodeldatalist (which it uses for itself
> when dropping).
>

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.

Thanks,

Rodrigo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210420/431e8604/attachment.htm>


More information about the PyQt mailing list