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

查看:701
本文介绍了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)

虚拟QVariant retrieveData(const QString& mimetype,QVariant :: Type type)const
{
if(mimetype匹配预期的&&类型匹配)
{
使用m_filename
}
}
}

延迟编码示例显示了此原则。



您可能还需要重写 hasFormat 格式以提供适当的类型, application / octet-stream 可能是你玩的最多的一个,你可能需要阅读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天全站免登陆