Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve all the file paths from a QDropEvent

Tags:

qt

I implemented drag and drop in my software. Basically I've got a QTableView which contains file paths. The user must be able to just drag and drop files in the QTableView in order to add them.

I've already done the big of the job but I'm stuck in iterating all the paths contained in the QDropEvent object. I have to implement the dropEvent method.

void Generous::dropEvent(QDropEvent *dropEvent) {
    QStringList filePathList;

    // Way to iterate dropEvent and append each file path to filePathList.

    addFilesToListView(filePathList);
}

How can I do that?


1 Answers

I would guess that a drag and drop using filepaths use the MIME type text/uri-list.

If this is true, you should probably be able to retrieve the data like this:

if (dropEvent->mimeData()->hasUrls())
{
    foreach (QUrl url, dropEvent->mimeData()->urls())
    {
        filePathList << url.toLocalFile();
    }
}

Anyway, since I am not sure, the best would be first to check what kind of info is stored inside the drop event, and see where and how you can extract the filepaths:

QStringList availableMimeTypes = dropEvent->mimeData()->formats();
qDebug() << "available MIME types:" << dropEvent->mimeData()->formats() << "\n";

foreach(QString mimeType, availableMimeTypes)
{
    qDebug() << "data for MIME type" << mimeType << " :";
    qDebug() << dropEvent->mimeData()->data(mimeType) << "\n";
}
like image 194
Boris Dalstein Avatar answered Oct 26 '25 04:10

Boris Dalstein