I was trying to use a simple drag and drop feature in my Qt app. Here is my code:
MyWindow::MyWindow(QWidget *parent)
{
..........
setAcceptDrops(true);
}
void MyWindow::dragEnterEvent(QDragEnterEvent *e)
{
if (e->mimeData()->hasUrls()) {
e->acceptProposedAction();
}
}
void MyWindow::dropEvent(QDropEvent *e)
{
foreach (const QUrl &url, e->mimeData()->urls()) {
const QString &fileName = url.toLocalFile();
qDebug() << "Dropped file:" << fileName;
}
}
As you see, it simply prints the path name of the file bing dropped into the console. So when I dragged and dropped a file from my desktop into the widget, I expected something like /Users/<myName>/Desktop/<filename>
in the console. But I see something like file:///.file/id=6571367.2773272/
being printed. And when I try to use it in some way, like opening the file (text) in my in-built editor, which was working fine for all OS-es except Os X Yosemite, the app crashes.
It is a known bug, as published here, with a patch here. But I don't know how to use the patch to make my code work. There seems to be a solution with an Objective C wrapper around Qt, however, I don't know how exactly to mix C++ in Qt and Objective C.
Any idea how do I use the patch, or make it work in some other way? Somehow I need to retrieve the actual full path of the file being dropped.
Environment - Os X Yosemite, Qt Creator 3.1.1 with Qt 5.2.1.
I will need to run the same app on Windows as well (we are developing in Qt for both Windows and Mac), so looking for cross-platform solution.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…