2014-11-23 45 views
6

我试图在我的Qt应用程序中使用简单的拖放功能。这是我的代码:Os X优胜美地Qt拖放文件名缺陷

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; 
    } 
} 

正如你所看到的,它只是打印放入控制台的文件bing的路径名。所以当我从桌面拖放一个文件到小部件中时,我预期在控制台中有类似/Users/<myName>/Desktop/<filename>的东西。但是我看到类似file:///.file/id=6571367.2773272/正在打印。当我尝试以某种方式使用它时,例如在我的内置编辑器中打开文件(文本),该文件适用于除Os X Yosemite之外的所有操作系统,该应用程序崩溃。

这是一个已知的bug,发布为here,补丁here。但我不知道如何使用该补丁来使我的代码工作。似乎有围绕Qt的Objective C包装的解决方案,但是,我不知道如何在Qt和Objective C中混合使用C++。

任何想法如何使用该补丁或使其工作其他方式?不知何故,我需要检索被删除文件的实际完整路径。

环境 - OS X优胜美地,Qt Creator 3.1.1与Qt 5.2.1。

我需要在Windows上运行相同的应用程序(我们正在为Windows和Mac开发Qt),因此寻找跨平台解决方案。

回答

4

权,这是一个普通的

如何修补了Qt源,以修复错误或添加新功能?

要求。这对整体文件可能是有用的,因此这里得到答案。

你可以抓住官方发布的tarball和补丁,没有混帐或者你可以通过存储库。我个人会选择第二个补丁,因为我觉得比较简单,用git进行樱桃采摘比较容易。这些都是你需要采取的步骤:

  1. 克隆了Qt 5库

    git clone git://gitorious.org/qt/qt5.git qt5 
    
  2. 转到克隆目录

    cd qt5 
    
  3. 初始化库

    perl init-repository 
    
  4. 转到你需要修补

    cd qtbase 
    
  5. 创建一个帐户格里特如果你有没有却​​把qtbase目录。这一步是可选的。

  6. a。取并从格里特

enter image description here

git fetch https://[email protected]/qt/qtbase refs/changes/11/92611/4 && git cherry-pick FETCH_HEAD 

b樱桃挑修复。不要创建一个gerrit帐户

这样做在这种情况下是可行的,因为它是对源代码的一个非常小的改变,其余的只是更改为基准。也没有预期的变更手段的更新。

* git apply 
    * copy and paste the following snippet to the standard input 

    commit 66a305f282e33b1bf12bec21f416c8ba6730cd40 
    Author: Cyril Oblikov <[email protected]> 
    Date: Tue Aug 19 16:18:25 2014 +0300 

     OSX: convert file reference url to path-based url 

     According to Apple's 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); 
     } 
  • 配置项目

    ./configure -developer-build -opensource -nomake examples -nomake tests 
    
  • 构建和安装项目

    make -j4 all install 
    
  • 去喝茶,直到它准备

  • +0

    让我们[在聊天中继续讨论](http://chat.stackoverflow.com/rooms/67486/discussion-between-cupidvogel-and-lpapp)。 – SexyBeast 2014-12-22 18:26:18

    +0

    仅供参考,我建议不要使用-J4。看起来使用-J4时可能会发生一些竞争条件并且无法建立。它更长,但我建议只使用-j1 – Seb 2015-01-12 16:45:40

    +0

    FYI,这已在Qt 5.4.1中修复。 [QTBUG-40449](https://bugreports.qt.io/browse/QTBUG-40449) – Shinnok 2015-03-17 08:39:48

    2

    应用补丁,你需要下载Qt的来源,然后添加使用检索文件路径,而不是FILEID所需要的线路(如使用git):

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

    方法。

    这里是应该补充的部分:Patch Code

    当你这样做,你需要建立Qt和与此补丁版本构建项目采取使用您所做的更改。