Qt 4.x:如何实现拖放到桌面或文件夹? [英] Qt 4.x: how to implement drag-and-drop onto the desktop or into a folder?

查看:653
本文介绍了Qt 4.x:如何实现拖放到桌面或文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Qt 4.x编写了一个使用C ++编写的文件传输应用程序,它记录到服务器中,向用户显示服务器上可用的文件列表,并允许用户上传或下载文件。



这一切都很好您甚至可以从桌面(或从打开的文件夹)拖动文件,并且将文件图标拖放到server-files-list-view中后,丢弃的文件将被上传到服务器。



现在我有一个相反的动作请求...我的用户希望能够将文件从服务器文件列表视图拖放到桌面上,或进入一个打开的文件夹窗口,并将该文件下载到该位置。



这似乎是一个合理的请求,但我不知道如何实现它。有没有办法让Qt应用程序找到与drop event发生的位置对应的目录,当图标被放到桌面或打开文件夹窗口时?理想情况下,这将是一个基于Qt的平台中立机制,但如果不存在,则MacOS / X和Windows(XP或更高版本)的平台特定机制就足够了。



任何想法?

解决方案

查看 QMimeData 它的文档,它有一个虚拟函数

  virtual QVariant retrieveData(const QString& mimetype,QVariant :: Type type)const 

这意味着你拖动到外面你相应地实现这个功能

  class DeferredMimeData:public QMimeData 
{
DeferredMimeData(QString downloadFilename):m_filename(downloadFilename)

virtual QVariant retrieveData(const QString& mimetype,QVariant :: Type type)const
{
if(mimetype匹配预期的&&类型匹配预期)
{
使用m_filename执行下载
}
}
}

延迟编码示例显示了这个原则。



您可能还需要覆盖 hasFormat 格式提供适当的类型,应用程序/八位字节流可能是那可能会让你发挥最大的一面,你可能必须先了解一下Windows专门使用mime类型处理拖放方式。



我不知道如何您将提供保存该文件的文件名,但您可能必须进入Windows的一面。查看 QWindowsMime 的来源可能也有帮助。可能需要一个多步骤的过程,您将获得文件名 text / uri-list 数据的请求,然后 application / octet-stream 为数据。



希望这有帮助


I've written a little file-transfer application written in C++ using Qt 4.x ... it logs into a server, shows the user a list of files available on the server, and lets the user upload or download files.

This all works fine; you can even drag a file in from the desktop (or from an open folder), and when you drop the file icon into the server-files-list-view, the dropped file gets uploaded to the server.

Now I have a request for the opposite action as well... my users would like to be able to drag a file out of the server-files-list-view and onto the desktop, or into an open folder window, and have that file get downloaded into that location.

That seems like a reasonable request, but I don't know how to implement it. Is there a way for a Qt application to find out the directory corresponding to where "drop event" occurred, when the icon was dropped onto the desktop or into an open folder window? Ideally this would be a Qt-based platform-neutral mechanism, but if that doesn't exist, then platform-specific mechanisms for MacOS/X and Windows (XP or higher) would suffice.

Any ideas?

解决方案

Look at QMimeData and its documentation, it has a virtual function

virtual QVariant retrieveData  ( const QString & mimetype, QVariant::Type type ) const

this means to do you drag to the outside you implement this functions accordingly

class DeferredMimeData : public QMimeData
{
  DeferredMimeData(QString downloadFilename) : m_filename(downloadFilename)

  virtual QVariant retrieveData (const QString & mimetype, QVariant::Type type) const 
  {
    if (mimetype matches expected && type matches expected)
    {
       perform download with m_filename
    }
  }
}

The delayed encoding examples shows this principle.

You will probably also have to override hasFormat and formats to provide the appropriate types, application/octet-stream probably being the one that might get you the most play, you will probably have to read up on how windows specifically handles drag and drop using mime types.

I don't know how you will supply the file name under which the file is saved, but you will probably have to get into the windows side of things. Looking at the source of QWindowsMime might also help. There might me a multiple step process where you will get requests for text/uri-list data for the filenames and then application/octet-stream for the data.

Hope this helps

这篇关于Qt 4.x:如何实现拖放到桌面或文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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