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?
source share