3
votes

In my own TextEdit (inherits QTextEdit) I implement this DragEventHandler:

void CustomTextEdit::dragEnterEvent(QDragEnterEvent* e)
{
    qDebug() << "void CustomTextEdit::dragEnterEvent(QDragEnterEvent* e)";
    qDebug() << "e->mimeData()->hasText() is" << e->mimeData()->hasText();

    QTextEdit::dragEnterEvent(e);
}

Example: When I select text inside the TextEdit and drag it around, the handler gets called and hasText() is true.

When I drag an item from a QListWidget into the TextEdit the handler also gets called but hasText() is false. How can I handle the DropEvent anyway? (QDragEnterEvent basically IS a QDropEvent)

I know that this would be done in the DropHandler but my question is what information does the DropEvent coming from QListWidget contain? How can access this information?

1

1 Answers

5
votes

You can ask a QDragEnterEvent which formats of mime data it contains using QMimeData::formats().

For example:

const QMimeData *mimeData = e->mimeData();
QStringList mimeFormats = mimeData->formats();
foreach(QString format, mimeFormats)
  qDebug() << format;

From this we can see that the event's mimeData has the format application/x-qabstractitemmodeldatalist.

The data of any particular format can be retrieved with QMimeData::data(QString mimeType), though in the case of application/x-qabstractitemmodeldatalist, the data is encoded in a pretty specific way. This previous SO question covers decoding the data from the QByteArray returned by QMimeData::data: How to decode "application/x-qabstractitemmodeldatalist" in Qt for drag and drop?