使用QMimeData在Qt中拖放数据 [英] Passing data around with QMimeData in Qt drag and drop
问题描述
我试图了解如何在Qt中使用拖放时传递数据。从我从一开始学习的例子中我了解到,你首先通过覆盖通过 QWidget
继承的方法来定义一个可拖动的小部件。
I'm trying to understand how data gets passed around when using drag and drop in Qt. From what I understood from the examples I've been studying, you first define a widget as draggable by overriding methods inherited through QWidget
.
在重写方法的实现中,我一直在寻找的示例实例化一个指向 QMimeData
对象的指针,并通过调用 setText(const QString& text)
和 setData(const QByteArray& data)
。他们将信息存储在 QByteArray
对象与<<
运算符:
In the implementation of the overridden method, the examples I've been looking at instantiate a pointer to a QMimeData
object, and store information in it by calling setText(const QString &text)
and setData(const QByteArray &data)
. They store information in the QByteArray
object with the <<
operator:
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << labelText << QPoint(ev->pos() - rect().topLeft());
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-fridgemagnet", itemData);
mimeData->setText(labelText);
在 dropEvent()
方法在接受滴下的窗口小部件中,这两个变量都使用>>
运算符进行检索:
In the definition of the dropEvent()
method in the widget that accepts the drops, both of those variables were retrieved with the >>
operator:
QString text;
QPoint offset;
dataStream >> text >> offset;
在 setData()
em> application / x-fridgemagnet 作为MIME类型参数传递。是否定义在别的地方或它只是你可以弥补的东西?
In the setData()
method, application/x-fridgemagnet was passed as a MIME type argument. Was that defined somewhere else or its just something you can make up?
如何在 QMimeData $中存储和检索自定义对象c $ c>对象?我试过这个:
How can I store and retrieve a custom object inside the QMimeData
object? I tried this:
dataStream << labelText << QPoint(ev->pos() - rect().topLeft()) << myObject;
,并尝试如下检索:
myClass myObject;
dataStream >> text >> offset >> myObject;
但它没有起作用,theres不符合'operator >>'。任何提示我应该怎么做?
But it didn't work, says theres "no match for 'operator >>'". Any tips on what should I do?
推荐答案
在setData()方法中,application / x -fridgemagnet作为MIME类型参数传递。是否定义在别的地方或它只是你可以弥补的东西?
In the setData() method, application/x-fridgemagnet was passed as a MIME type argument. Was that defined somewhere else or its just something you can make up?
如果数据是你自己的专有格式,那么你可以弥补但是,如果它是标准化的,像图像,你可能想使用一个已知的mime类型。
If the data is in your own proprietary format, then you can make it up. If, however, it's something standardized, like images, you'll probably want to use a known mime-type.
如果你已经支持序列化到XML,那么它将很容易创建自己的mime类型,序列化到XML,然后在接收端解除序列化。
If you already support serialization to XML, then it would be easy to create your own mime-type, serialize to XML, and then de-serialize on the receiving end.
我如何存储并在QMimeData对象内检索自定义对象?
How can I store and retrieve a custom object inside the QMimeData object?
您需要创建非成员运算符(<和& >)以QDataStream支持的方式写出 MyObject
的成员数据。请参阅阅读和撰写其他Qt类标题下的QDataStream 文档。
You'll need to create non-member operators (<< and >>) that write out MyObject
's member data in a way supported by QDataStream. See the QDataStream documentation under the heading "Reading and Writing other Qt Class."
这将涉及创建以下两种方法:
That will involve creating the following two methods:
QDataStream &operator<<(QDataStream &, const MyObject &);
QDataStream &operator>>(QDataStream &, MyObject &);
由于这些是非成员运算符,因此它们将在您的类之外定义:
Since these are non-member operators, they will be defined outside your class:
class MyObject { /* ... */ };
QDataStream &operator<<(QDataStream &stream, const MyObject &obj) {
/* as long as first_member and second_member are types supported
by QDataStream, I can serialize them directly. If they're not
supported, I'd need an operator for them as well unless I can
convert them to a QString or something else supported by Qt /
QDataStream */
stream << obj.first_member;
stream << obj.second_member;
/* and so on and so forth */
return stream;
}
/* and similarly for operator>> */
这篇关于使用QMimeData在Qt中拖放数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!