Os X优胜美地Qt拖放文件名错误 [英] Os X Yosemite Qt drag and drop file name bug

查看:146
本文介绍了Os X优胜美地Qt拖放文件名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的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:<<文件名;
}
}

如你所见,它只是打印出文件bine掉入控制台。所以当我将一个文件从桌面拖放到窗口小部件中时,我在控制台中预期会出现类似于 / Users /< myName> / Desktop /< filename> 的内容。但是我看到像 文件:///.file/id=6571367.2773272/ 正在打印。当我尝试以某种方式使用它时,像在内置编辑器中打开文件(文本),除了 Os X Yosemite 之外的所有操作系统都可以正常工作,应用程序崩溃。



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



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



环境 - Os X Yosemite,Qt Creator 3.1.1 with Qt 5.2.1。



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

解决方案

对,这是一个通用的


补丁Qt源,以修复错误或添加功能?


请求。这可能是有用的一般文件,因此这里是答案。



你可以抓住官方发布的tarball和补丁,没有git或你可以通过存储库。我个人选择第二个,因为补丁和樱桃采摘与git在我的愚蠢的意见更容易。这些是您需要采取的步骤:


  1. 克隆Qt 5存储库

      git clone git://gitorious.org/qt/qt5.git qt5 


  2. 转到克隆的目录

      cd qt5 


  3. 初始化存储库

      perl init-repository 


  4. 转到需要补丁的qtbase目录



      cd qtbase 


  5. 如果您还没有,请创建gerrit帐户。此步骤是可选的。


  6. a。从Gerrit获取并选择修复程序


  git fetch https:// yourgerritusername @ codereview。 qt-project.org/qt/qtbase refs / changes / 11/92611/4&& git樱桃选择FETCH_HEAD 

b。不要创建gerrit帐户



在这种情况下这将是可行的,因为它是源代码的一个很小的变化,其余只是更改为基准。

  * git申请
*复制并粘贴以下片段到标准输入

提交66a305f282e33b1bf12bec21f416c8ba6730cd40
作者:Cyril Oblikov< munknex@gmail.com&
日期:Tue Aug 19 16:18:25 2014 +0300

OSX:将文件引用URL转换为基于路径的URL

根据Apple的规范URL可以是:
基于路径的URL:file://localhost/Users/steve/Documents/MyFile.txt

文件引用URL:file:///.file/id=6571367.2773272 /

在拖放中,将OSX 10.10文件引用URL复制到剪贴板。

此修补程序将文件引用URL转换为基于路径的URL。

对性能的评论:在Macbook Air 2013上转换1000个网址需要
约15 ms。

添加了基准。

更改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 


  1. 构建并安装项目

      make -j4 all install 


  2. 它已经准备好了



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.

解决方案

Right, this a generic

How do I patch the Qt source in order to fix a bug or add a feature?

request. This may be useful to document in general, therefore here goes the answer.

You could grab the official release tarball and patch that without git or you can go through the repository. I would personally opt for the second since patching and cherry-picking with git is easier in my humble opinion. These are the steps that you need to take:

  1. Clone the Qt 5 repository

    git clone git://gitorious.org/qt/qt5.git qt5
    

  2. Go to the cloned directory

    cd qt5
    

  3. Initialize the repository

    perl init-repository
    

  4. Go to the qtbase directory which you need to patch

    cd qtbase
    

  5. Create a gerrit account if you have none yet. This step is optional.

  6. a. Fetch and cherry-pick the fix from Gerrit

    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 it is a very minor change to the source code, and all the rest is just change to the benchmarks. There are no expected updates to the craft of the change, either.

    * 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'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);
         }

  1. Configure the project

    ./configure -developer-build -opensource -nomake examples -nomake tests
    

  2. Build and install the project

    make -j4 all install
    

  3. Go get some tea until it is ready

这篇关于Os X优胜美地Qt拖放文件名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆