PDA

View Full Version : Drag and Drop from One QGraphicsScene to another QGraphicsScene



LSantoshKSingh
14th May 2017, 23:37
I have two different custom QGraphicsScene presented by means of two different QGraphicsView. I want to drag and drop items from one scene to another. Therefore I have implemented following in my QGraphicsScene:

void mousePressEvent
void mouseMoveEvent
void dragEnterEvent
void dragMoveEvent
void dragLeaveEvent
void dropEvent


I want to reject drop; if it is same Scene I am dragging from and dropping into. For this; I need to get QGraphicsSceneDragDropEvent::event->source() and cast it to a proper widget and see if it has originated from the same object by comparing it with 'this' pointer.


void my_Chart::dragEnterEvent(QGraphicsSceneDragDropEve nt *event)
{

my_Chart *chart= qobject_cast<my_Chart *> (event->source());
if(chart != 0)
{

if(chart != this)

// do something relevent
else

//reject drop
}
else
{

//reject drop
}

}

The casting is always failing, whatever I do. Chart is always 0. I have checked documentation, It says that:http://doc.qt.io/qt-5/qgraphicsscenedragdropevent.html#source
This function returns the QGraphicsView that created the QGraphicsSceneDragDropEvent. So I tried:

my_ChartView *chart= qobject_cast<my_ChartView *> (event->source());

But this also failed. Kindly help.

d_stranz
15th May 2017, 20:28
Create a temporary QObject * variable in your event and assign event->source() to it. Set a breakpoint in the debugger after that assignment and examine the QObject pointer to see what it actually points to. Alternatively, insert a qDebug() statement to print the value of event->source() (which will also tell you what it points to).

LSantoshKSingh
16th May 2017, 15:18
Thank you.