PDA

View Full Version : drop event becomes invalid



lwz
28th March 2014, 09:45
Using drag and drop to drag tree item to a QwtPlot, sometimes the event becomes invalid.

Please help. Below is the my code about drag and drop.



Drag starts:

void DataTree::mouseMoveEvent(QMouseEvent *event)
{
if (m_currentItem) {
QString name = m_currentItem->text(TITLE_COLUMN);
QString seq = m_currentItem->text(ID_COLUMN);
QString type = m_currentItem->text(TAG_COLUMN);
QString code = m_currentItem->text(CODE_COLUMN);

if (code.length() == 8) {
QTreeWidgetItem *parentItem = m_currentItem->parent();
QString fileName = parentItem->text(TAG_COLUMN);

// Prepare for dragging
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QByteArray byteArray;
QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
dataStream << fileName << name << seq << type;

QSize size(30,30);
QPixmap widgetImage = m_currentItem->icon(0).pixmap(size);
drag->setPixmap(widgetImage);
mimeData->setData("curveData/x-curveData", byteArray);
drag->setMimeData(mimeData);

drag->setHotSpot(QPoint(15,15));
if (!(drag->exec(Qt::MoveAction) == Qt::MoveAction))
{
// event->ignore();
;
}
}

void DataTree::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("curveData/x-curveData"))
{
event->acceptProposedAction();
event->accept();
}
else
{
event->ignore();
}
}


void DataTree::dragMoveEvent(QDragMoveEvent *event)
{
if( event->mimeData()->hasFormat("curveData/x-curveData")){
event->acceptProposedAction();
event->accept();
}
else
{
event->ignore();

}

}

Accept drop:

void Plot::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("curveData/x-curveData")) {
QByteArray byteArray = event->mimeData()->data("curveData/x-curveData");
QDataStream dataStream(&byteArray, QIODevice::ReadOnly);

QString fileName;
QString name ;
QString seq;
QString type;

dataStream >> fileName >> name >> seq >> type;

emit addCurve(fileName, name, seq, type, this);
event->acceptProposedAction();
event->accept();

} else {
event->ignore();
}
}

void Plot::dragEnterEvent(QDragEnterEvent *event)
{
if ( event->mimeData()->hasFormat("curveData/x-curveData")) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->ignore();

}
}

void Plot::dragMoveEvent(QDragMoveEvent *event)
{

if (event->mimeData()->hasFormat("curveData/x-curveData")) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->ignore();
}
}

anda_skoa
30th March 2014, 12:36
What do you mean with "it becomes invalid"?
Where in your code does that lead to a problem?

Cheers,
_

lwz
31st March 2014, 02:31
When I drag an tree item to my plot, I'm sure the drag event was triggered since the mouse was moving with the item icon which I set as the drag picture. But when releasing the mouse which should had triggered the drop event, nothing happened.

anda_skoa
31st March 2014, 10:06
Do you get the drag enter and move events?

Cheers,
_

lwz
1st April 2014, 02:03
yeah, both of drag enter and move events are accepted. But no drop event .

anda_skoa
1st April 2014, 08:51
Ah, so the problem is that Plot::dropEvent() is not called, not that it is called with an invalid event, right?

Have you switched the widget's acceptDrop property to true?

Cheers,
_

lwz
1st April 2014, 09:29
this->setAcceptDrops(true);

I have set this property, the problem above happens occasionally. The program runs fine most of the time.

lwz
3rd April 2014, 15:47
what else can I do?