How to pass or retrieve (Mime) data when dragging/dropping a QTreeWidgetItem

43 views Asked by At

I am attempting to drag and drop an item from a QTreeWidget in a QGraphicsScene. I want to get data other than just the item title.

My item already has some data set with the setData(int,int,QVariant) function:

item->setData( 1, Qt::UserRole, QJsonArray);

I need to get the JSon variant to be able to process my drop event in the scene.

In the scene, I have overridden the dragEnterEvent with a QStandardItemModel shared in this answer on Drag and Drop QTreeWidgetItem to QGraphicsView with custom data.

void main_scene::dragEnterEvent(QGraphicsSceneDragDropEvent* event) {
    bool acceptDrag = true;

    if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) {
        QStandardItemModel dummy_model;
        if (dummy_model.dropMimeData(event->mimeData(), event->dropAction(), 0, 0, QModelIndex()) ){
            QModelIndex index = dummy_model.index(0, 0);
                qDebug() << index.data();
        }
    }

    event->setAccepted(acceptDrag);
}

Which only gives me the item title:

QtDebug: QVariant(QString, "Item title")

I didn't find any way to retrieve the data of the item. Or to directly set MimeData. Usually I do something like override the mouseMoveEvent and set mimedata to a QDrag Object, which I didn't find possible in QTreeWidget.

How do I drag and drop a QTreeWidgetItem to a scene with more metadata than only the item title?

0

There are 0 answers