Lars Karlsson
Lars Karlsson

Reputation: 11

Dragging multiple QGraphicsItems in different QGraphicsScenes for the same amount

I have my own classes (MyGraphicsScene, MyGraphicsView, MyGraphicsItem) derived from QGraphicsScene, QGraphicsView and QGraphicsItem.

In my main window, I then create nine (9) instances of MyGraphicsScene, shown through nine instances of MyGraphicsView. All nine MyGraphicsScenes contain pointers to each other.

How can I drag an instance of MyGraphicsItem in one MyGraphicsScene, and then automatically drag certain MyGraphicsItem instances (in the eight remaining MyGraphicsScenes) for the same amount/distance/vector?

My first idea was to reimplement MyGraphicsItem::itemChange (with change == QGraphicsItem::ItemPositionChange) and then call setPos() for remaining instances of MyGraphicsItem (contained within other MyGraphicsScenes). However this won't work because I will get infinite recursion (setPos() would trigger itemChange() as well, including for the originating MyGraphicsItem).

Any other ideas from experienced Qt-ers?

Upvotes: 1

Views: 282

Answers (2)

A. Jones
A. Jones

Reputation: 68

I do know for a fact you can use the same QGraphicsScene in multiple views. So if you're just trying to have multiple views of the same thing, call MyGraphicsView all with the same scene.

Upvotes: 1

felixgaal
felixgaal

Reputation: 2423

If you define derived class for QGraphicsItem (I think you already have, right?) and reimplement the mouseMoveEvent so it calls the move method to all 9 objects.

Remember that you have at your disposal all the attributes of QMouseEvent, e.g. buttons(), pos().

void MyGraphicsItem::mouseMoveEvent( QMouseEvent *event )
{
  if (event->buttons() & Qt::LeftButton) {
    QPoint pos = event->pos();
    // ...
  }
}

Hope that helped!

Upvotes: 0

Related Questions