PDA

View Full Version : Moving child QGraphicsItem



roband915
27th February 2011, 13:19
With the mouse I create a polyline. That's the first QGraphicsItem. For every click with the mouse I create a child Item which draws an ellipse on every position I clicked the Mouse. The ellipse is movable and the purpose is to modify the polyline. When I move the ellipse the polyline is modified indeed but not following the ellipse. It has a big offset from the ellipse. What am I missing here?

Parent item:


itemSpline::itemSpline(QGraphicsItem *parent)
: QGraphicsPolygonItem(parent)
{
// TODO Auto-generated constructor stub
//setAcceptHoverEvents(true);
// setFlags(ItemIsMovable | ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
setFlags(QGraphicsItem::ItemIsSelectable);
setZValue(20.);
myColor = Qt::black;
isFinished = false;
}

QVariant itemSpline::itemChange(GraphicsItemChange change, const QVariant &value)
{
if(ItemPositionHasChanged)
{
//Should I do something here?
}
return QGraphicsPolygonItem::itemChange(change, value);
}

void itemSpline::alterPolygon(QPointF pos, int index)
{
qDebug("alterPolygon");

//Calculate offset and change boundingRectangle?
prepareGeometryChange();
vVertices[index] = pos;
}


Child item: (Ellipse)


Node::Node(QPointF nodePos, itemSpline *parent, int ind)
: QGraphicsItem(parent), splineItem(parent)
{
// TODO Auto-generated constructor stub
newPos = mapToParent(nodePos);

index = ind;
setAcceptsHoverEvents(true);
setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
}

QRectF Node::boundingRect() const
{
return QRectF(newPos.x()-4, newPos.y()-4, 8,8);
}

//This does nothing?
bool Node::advance()
{
qDebug("Node: Advance");

if (newPos == pos())
return false;

setPos(newPos);

return true;
}

QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
//only if polyline is finished!
if(splineItem->isFinished)
{
if(ItemPositionHasChanged)
{
QPointF valuePos(value.toPointF());

splineItem->alterPolygon(mapToParent(valuePos), index);

}
}

return QGraphicsItem::itemChange(change, value);
}