i have implemented the mousePress, mouseMove and mouseRelase in the QGraphicsView and i have added a QGraphicsWidget and a QGraphicsLayoutItem in it and added to the view.
now inside the graphicsLayoutitem i have implemented
void ParentItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
m_mousePressed = true;
QGraphicsObject::mousePressEvent(event);
}
void ParentItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
{
QGraphicsObject::mouseMoveEvent(event);
return;
}
if( this->boundingRect().contains(event->pos()) && (m_mousePressed))
{
QGraphicsObject::mouseMoveEvent(event);
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
// mime stuff
mimeData->setText("Parent");
drag->setPixmap(m_currentImage.scaled(30,30));
drag->setHotSpot(QPoint(15, 20));
drag->setMimeData(mimeData);
// start drag
drag->start(Qt::CopyAction | Qt::MoveAction);
}
so when i mouseMove in parentitem the Drag is been exec succesfully and when i drop the item in scene , the graphicsView is not getting the mouseRelease event. The graphicsview mouseRelease event is not been called when i drop it on scene.
this is how i handle the drop in scene
void HandlerScene::dropEvent ( QGraphicsSceneDragDropEvent * event )
{
if (event->mimeData()->hasText()){
if(event->mimeData()->text() == "Parent")
{
//My code
}
event->acceptProposedAction();
}
The sample code i have uploaded in the http://filesave.me/file/53135/DropItem-zip.html
You can Drag and drop on the item and it will create a new item. It works fine but when i give setFocus(Qt::mouseFocusReason) to the item the actual problem starts.
user on double clicking on the Text of the item i added a Qgraphicstextitem on top of it. when the textitem lost focus im deleting it.
In normal condition without double clicking on the item text,the drag and drop is working fine.
but when i add the QGraphicsTextItem on the top of the item and set the focus as setFocus(Qt::mouseFocusReason) and if i again drag the item the QGraphicsView mouseMove event is keep on calling.