2

I have a QMainWindow with two QGraphicsView's each owning a QGraphicsScene. Both views are displayed (on screen) constantly. I would like to be able to drag and drop objects (objects of a class subclassed from QGraphicsItem) from one QGraphicsView to the other. What's the best way to do this?

ps: I can drag and drop inside one QGraphicsView

Mat
  • 202,337
  • 40
  • 393
  • 406
mmvie
  • 2,571
  • 7
  • 24
  • 39

1 Answers1

2

In the views mouse event create a new drag object to contain the data you want moved, for example:

QDrag* drag = new QDrag( this );
QByteArray ba;
QDataStream* data = new QDataStream(&ba, QIODevice::WriteOnly);
*data << m_slideIndex;
QMimeData* myMimeData = new QMimeData;
    myMimeData->setData("application/x-thumbnaildatastream", ba);
drag->setMimeData( myMimeData );
drag->setPixmap( thumb );
drag->setHotSpot( thumb.rect().center() );
if ( drag->exec() == Qt::IgnoreAction )
{
    qDebug() << "DRAG CANCELLED";
    m_dragging = false;
}
drag->deleteLater();
delete data;

And then in the QGraphicsScene's dropEvent() implement the catch for that data.

Phil Hannent
  • 12,047
  • 17
  • 71
  • 118