Search code examples
c++qtqgraphicsview

QGraphicsEllipseItem not movable


I'm working in a MAC OS X / QT5.3 widget application and i'm trying to make QGraphicsEllipseItem in QGraphicsItemGroupmovable. I'm setting all the needed flags, but the ellipse won't move.

Here's my code (omitted unimportant and simplified):

 QGraphicsView * view;
 QGraphicsItemGroup * controlPoints;
 //there are more groups

 void setUp()
 {
    view = new QGraphicsView();
    view->setRenderHint(QPainter::Antialiasing,true);

    QGraphicsScene * scene = new QGraphicsScene(0,0,500,500);
    view.setScene(scene);

    controlPoints = new QGraphicsItemGroup();
    //there are multiple groups and i need them to be in different depths(zvalues)
    controlPoints->setZValue(2);

    scene.addItem(controlPoints);
 }

 void AddPointsToGroup()
 {
    QGraphicsEllipseItem * controlPoint = new QGraphicsEllipseItem(100,100,6.5,6.5);
    controlPoint->setFlag(QGraphicsItem::ItemIsMovable,true);
    controlPoint->setFlag(QGraphicsItem::ItemIsSelectable,true);
    controlPoint->setFlag(QGraphicsItem::ItemIsFocusable,true);
    controlPoint->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
    controlPoints->addToGroup (controlPoint);
 }

Now, the point is shown ,but not movable.


Solution

  • As the Qt documentation states:-

    The QGraphicsItemGroup class provides a container that treats a group of items as a single item.

    Therefore, you shouldn't expect to move a single item within the group. If you want to move the group as a whole, you'd need to set the flags on the group itself.

    As a QGraphicsItem and derived classes can be parented, you can achieve the desired effect by creating a single QGraphicsItem which you use as a parent to all those you would want to group together.

    This would then allow moving of the separate graphics item members.