Passing data using QMimeData in Qt drag and drop

I am trying to understand how data is transferred when using drag and drop in Qt. From what I understood from the examples I studied, you first define the widget as draggable by overriding the methods inherited through QWidget .

In the implementation of the overridden method, the examples I was looking for create an instance of a pointer to a QMimeData object and store information in it, calling setText(const QString &text) and setData(const QByteArray &data) . They store information in a QByteArray using 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); 

In the definition of the dropEvent() method in the widget that accepts drops, both of these variables were obtained using the >> operator:

 QString text; QPoint offset; dataStream >> text >> offset; 

In the setData() method, the application / x -fridgemagnet was passed as an argument of type MIME. Was it defined somewhere else, or just something you could do?

How to save and get a custom object inside a QMimeData object? I tried this:

 dataStream << labelText << QPoint(ev->pos() - rect().topLeft()) << myObject; 

and tried to get it like this:

 myClass myObject; dataStream >> text >> offset >> myObject; 

But that did not work, says theres "there is no match for the operator β†’". Any tips on what I should do?

+4
source share
2 answers

In the setData () method, the application / x -fridgemagnet was passed as an argument of type MIME. Was it defined somewhere else, or just something you could do?

If the data is in your own proprietary format, you can do it. If, however, something is standardized, such as images, you probably want to use the famous mime type.

If you already support serialization in XML, then it would be easy to create your own mime type, serialize in XML and then de-serialize on the receiving side.

How to save and get a custom object inside a QMimeData object?

You will need to create non-member statements (<<and β†’) that write out the MyObject element data in a manner supported by QDataStream. See the QaataStream Documentation under the heading "Reading and Writing Another Qt Class".

This will require the creation of the following two methods:

 QDataStream &operator<<(QDataStream &, const MyObject &); QDataStream &operator>>(QDataStream &, MyObject &); 

Since they 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>> */ 
+5
source

Yes, you can create your own MIME type for use in your own application. Obviously, no external applications will understand what it is, so you cannot drag outside your own application.

As for storing your own object, you need to define a stream operator, since Qt knows nothing about your object. QDataStream only defines stream operators for simple built-in types. You will need to define something like

 QDataStream& operator<<( QDataStream& ds, const myClass& myObject ) { // Use existing QDataStream stream operators or other methods to serialise your object // ... // ... return ds; } 

Similarly, for de-serialization, you need to define the appropriate operator>> .

+3
source

Source: https://habr.com/ru/post/1302075/


All Articles