[Interest] Qt->Qml Drag and Drop not working

Jack O'Neill jackneill1000 at gmail.com
Fri Nov 3 18:35:29 CET 2017


Hey,

I'd like to request help with the following problem:

I have a custom QAbstractItemModel derived model:

QStringList NodesTreeModel::mimeTypes() const
{
    return {"application/nodeitem"};
}

QMimeData *NodesTreeModel::mimeData(const QModelIndexList &indexes) const
{
    QMimeData *mimeData = new QMimeData;
    QByteArray encodedData;
    QDataStream stream(&encodedData, QIODevice::WriteOnly);

    foreach (QModelIndex index, indexes) {
        if (index.isValid()) {
            QString text = data(index, Qt::DisplayRole).toString();
            stream << text;
        }
    }
    mimeData->setData("application/nodeitem", encodedData);
    return mimeData;
}

Qt::ItemFlags NodesTreeModel::flags(const QModelIndex &index) const
{
    if (!index.isValid()) {
        return nullptr;
    }
    return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled |
           Qt::ItemIsDropEnabled;
}

Which is used by a QTreeView. dragDropMode is InternalMove.
I also have the following qml:

DropArea {
        anchors.fill: parent
        keys: ["application/nodeitem"]

        onEntered: {
            if (drag.formats.indexOf("application/nodeitem") >= 0) {
                drag.accept()
                print(drag.getDataAsString("application/nodeitem")) //
prints nothing
            }
        }

        onDropped: { // does not even run
            if (drop.formats.indexOf("application/nodeitem") >= 0) {
                print("dropped")
                drop.accept()
                print(drop.getDataAsString("application/nodeitem"))
            }
        }
    }

Why does drag.getDataAsString("application/nodeitem") not return the string?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171103/e8755d10/attachment.html>


More information about the Interest mailing list