Os X Yosemite Qt drag file name error

I am trying to use a simple drag and drop function in my Qt application. 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 can see, it just prints the path name of the file being dropped to the console. Therefore, when I dragged a file from the desktop to the widget, I expected something like /Users/<myName>/Desktop/<filename> in the console. But I see something like file:///.file/id=6571367.2773272/ . And when I try to use it in some way, for example, to open a file (text) in my built-in editor, which works fine for all OSs except Os X Yosemite , the application crashes.

This is a known bug posted here with the patch here . But I do not know how to use the patch to make my code work. There seems to be a solution with Objective-C wrapping around Qt, however I don't know how to mix C ++ in Qt and Objective-C exactly.

Any idea how I can use the patch or make it work in some other way? Somehow I need to restore the actual full path to the file to be deleted.

Environment - Os X Yosemite, Qt Creator 3.1.1 with Qt 5.2.1.

I also need to run the same application on Windows (we are developing Qt for Windows and Mac), so we are looking for a cross-platform solution.

+6
source share
2 answers

That's right, this is common

How do I fix a Qt source to fix a bug or add a function?

request. This can be useful for documentation in general, so the answer is here.

You can grab the official tarball and patch release without git, or you can go through the repository. I personally would prefer the second one, since fixing and selecting a cherry using git is easier in my humble opinion. These are the steps you need to take:

  • Clone Qt 5 repository

     git clone git://gitorious.org/qt/qt5.git qt5 
  • Go to the cloned directory

     cd qt5 
  • Initialize Repository

     perl init-repository 
  • Go to the qtbase directory you need to schedule

     cd qtbase 
  • Create a gerrit account if it is not already. This step is optional.

  • a. Fetch and cherry-pick fix from Gerrit

enter image description here

  git fetch https:// yourgerritusername@codereview.qt-project.org /qt/qtbase refs/changes/11/92611/4 && git cherry-pick FETCH_HEAD 

b. Do not create a gerrit account

This will be feasible in this case, since this is a very minor change in the source code, and everything else is just a transition to the standards. Also, the expected updates for the ship change are not expected.

  * git apply * copy and paste the following snippet to the standard input commit 66a305f282e33b1bf12bec21f416c8ba6730cd40 Author: Cyril Oblikov < munknex@gmail.com > Date: Tue Aug 19 16:18:25 2014 +0300 OSX: convert file reference url to path-based url According to Apple spec URL can be: path-based URL: file://localhost/Users/steve/Documents/MyFile.txt or file reference URL: file:///.file/id=6571367.2773272/ On OSX 10.10 file reference urls are copied to clipboard during Drag&Drop. This patch converts file reference url to path-based url. Comment on performance: converting 1000 urls on Macbook Air 2013 takes about 15 ms. Also benchmark is added. Change-Id: Ia42386cd90d1eb11d04ab33705f5eece6c13f370 diff --git a/src/platformsupport/clipboard/qmacmime.mm b/src/platformsupport/clipboard/qmacmime.mm index 6fcd19e..2bb623f 100644 --- a/src/platformsupport/clipboard/qmacmime.mm +++ b/src/platformsupport/clipboard/qmacmime.mm @@ -614,6 +614,8 @@ QVariant QMacPasteboardMimeFileUri::convertToMime(const QString &mime, QList<QBy QUrl url = QUrl::fromEncoded(data.at(i)); if (url.host().toLower() == QLatin1String("localhost")) url.setHost(QString()); + if (url.host().isEmpty() && url.path().startsWith(QLatin1String("/.file/id="))) + url = QUrl::fromNSURL([url.toNSURL() filePathURL]); url.setPath(url.path().normalized(QString::NormalizationForm_C)); ret.append(url); } 
  1. Customize project

     ./configure -developer-build -opensource -nomake examples -nomake tests 
  2. Create and install a project

     make -j4 all install 
  3. Make tea until it is ready

+4
source

To apply the patch, you need to download Qt Sources (for example, use git), and then add the lines needed to get the file, not fileId, using:

 QUrl::fromNSURL([url.toNSURL() filePathURL]); 

method.

Here is the part where it should be added: Patch Code

When you have done this, you need to create Qt and create a project with this fixed version in order to use the changes you made.

+2
source

Source: https://habr.com/ru/post/978588/


All Articles