Nominate our 2022 Qt Champions!

Question about overriding QAbstractItemModel::mimeData()

  • The argument to this function is const QModelIndexList &indexes, and the return value is a pointer to QMimeData.

    If there is more than one QModelIndex in the list, obviously I must somehow make a list or an array of the corresponding QMimeData data elements before returning from this function.

    So my question is this: What is the expected format of the object behind the QMimeData pointer? Is it an array, or a single object?

    If it is an array, do I have to delimit it with a "null" (or default initialized) QMimeData object?
    If it is a single object, I assume that I must somehow stream the data for ALL the QModelIndex's into a QByteArray (or perhaps a QJsonDocument and export that as a QByteArray)?

    What if I have a custom view and some columns hold text, while others might hold images? Does all of this data have to fit into a single object of type QMimeData assuming, e.g. that I select several rows of two columns each (with different types of data) and try to drag them to a different widget?

    The documentation could perhaps be a little more specific here.

  • Lifetime Qt Champion


    It is whatever you need to pass your data around. The receiving class must be able to decode it.

    One interesting example for big data structure is the Qt 4.8 Drag And Drop delayed encoding example.

  • @sgaist : Thanks ... I realized just now that the documentation of this function becomes quite clear when one looks at the source code of QAbstractItemModel::mimeData() in the file "../qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp" at line 1936.

    It even says "Returns an object (...)" -- i.e., all one really needs to know.

Log in to reply