Stray ‘/’ in path when querying MIME url list in Qt Drag and Drop

In Qt, when an object is dropped onto a widget the following event handler is to be used to get the list of URLs of the dropped objects-
 

void Dialog::dropEvent(QDropEvent *event)

{

if (mimeData->hasUrls()) {

QList urlList = mimeData->urls();

qDebug() << urlList.at(0).path();

}

}


This code, according to the Drop Site example in Qt Demo (see below) should print the filename of the first file dropped.

dropSiteQtDemo

Following is the output-

1. On Linux
/home/user/Desktop/pic1.jpg

2. On Windows
/C:/Users/Abcd/Desktop/pic1.jpg

But this was not expected,
As you might have noticed, there is a stray '/' in the beginning of the path which makes the filename unusable. This makes the Drop Site example to misbehave when run in Windows.
A better alternative is to use urlList.at(0).toLocalFile() instead of urlList.at(0).path(); and the problem gets solved.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s