正确的方法来复制QMimeData对象 [英] Proper way to copy a QMimeData object
问题描述
我正在开发一个Qt应用程序,以存储剪贴板中的所有内容,以便以后恢复.我的方法是从 QApplication:中检索 QMimeData :剪贴板()
并将其存储在 QList< QMimeData *>
中.由于剪贴板中的数据易变,因此我必须复制 QClipboard :: mimeData()
返回的QMimeData.没有QMimeData的复制构造函数,所以我认为我会像这样复制它:
I am developing a Qt application to store whatever goes through the clipboard so I can restore it later. My approach was to retrieve the QMimeData from the QApplication::clipboard()
and store it in a QList<QMimeData *>
. As the data in the clipboard is volatile, I have to copy the QMimeData returned by QClipboard::mimeData()
.
There is no copy constructor for QMimeData so I figured that I would copy it like this :
const QMimeData * clipboardData = _clipboard->mimeData();
QMimeData * mimeCopy = new QMimeData();
foreach(const QString & format, clipboardData->formats())
mimeCopy->setData(format, clipboardData->data(format))
其中 _clipboard
是 QApplication :: clipboard()
.
除了某些特定于应用程序的MIME类型之外,此方法工作得还不错.例如,我注意到当我在Skype对话中复制,还原然后粘贴Skype消息时,该消息不再被引用".这是否证明我的副本有缺陷?有没有更好,更准确的方法来复制QMimeData?
This works relatively fine except for some application-specific MIME types. For example, I noticed that when I copy, restore and then paste a Skype message in a Skype conversation, the message is not "quoted" anymore. Does This proves that my copy is flawed ? Is there a better, more accurate way to copy QMimeData ?
经过一些调试,我发现Skype消息mimedata中有两种格式.包含消息本身文本的 Text/Plain
类型和包含一些类似xml的 application/x-qt-windows-mime; value ="SkypeMessageFragment"
类型数据. Qt关于MIME类型的文档指示 value ="..."
描述数据的编码方式.为了使QMimeData的副本有效,我是否必须在某些时候进行编码或解码?
After some debugging, I found out that there are two formats in a Skype message mimedata.
A Text/Plain
type containing the text of the message itself and a application/x-qt-windows-mime;value="SkypeMessageFragment"
type which contains some xml-like data. Qt's documentation on MIME types indicates that the value="..."
describes how the data is encoded. Do I have to encode or decode something at some point to make my QMimeData's copy valid ?
推荐答案
对于自定义MIME类型(例如 application/x-qt-windows-mime; value ="SomeValue"
),实际格式名称为实际上是 SomeValue
(在两个引号之间 value =
之后是什么).
For custom MIME types like application/x-qt-windows-mime;value="SomeValue"
, the real format name is in fact SomeValue
(what is after value=
, between the two quotes).
复制QMimeData对象的更准确方法如下:
A more accurate way to copy a QMimeData object would then be something like this :
QMimeData * copyMimeData(const QMimeData * mimeReference)
{
QMimeData * mimeCopy = new QMimeData();
foreach(QString format, mimeReference->formats())
{
// Retrieving data
QByteArray data = mimeReference->data(format);
// Checking for custom MIME types
if(format.startsWith("application/x-qt"))
{
// Retrieving true format name
int indexBegin = format.indexOf('"') + 1;
int indexEnd = format.indexOf('"', indexBegin);
format = format.mid(indexBegin, indexEnd - indexBegin);
}
mimeCopy->setData(format, data);
}
return mimeCopy;
}
这篇关于正确的方法来复制QMimeData对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!