PDA

View Full Version : Moving Items with itemChange?



konvex
20th November 2008, 18:14
Hi,
I have problems with itemChange. I use it like the example "Diagram Scene".
I have two ellipse and a line between them and if I move the ellipse the line should move too.
ellipseitem.cpp


QVariant EllipseItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == QGraphicsItem::ItemPositionChange)
{
foreach (LineItem *arrow, arrows)
{
arrow->updatePosition();
}
}
return value;
}

ellipseitem.h


...
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private:
QList<LineItem *> arrows;


but I get
LineItem was not declared in this scope
C++ forbids declaration of arrows with no type

My LineItem is like this: LineItem(qreal x1, qreal y1, qreal x2, qreal y2);
Can somebody tell me whats wrong?

caduel
20th November 2008, 18:16
did you #include your LineItem in the .cpp file?

konvex
20th November 2008, 18:25
Yes I did. That's why I don't understand why it is not declared...

konvex
20th November 2008, 20:57
no idea? Please help...

aamer4yu
21st November 2008, 05:28
can u show some more code..
and which line exactly are u getting the error ?

konvex
21st November 2008, 15:36
In my scene I only make an ellipse that is movable when I use the mouse.
ellipseitem.h


class EllipseItem : public QGraphicsEllipseItem
{
public:
EllipseItem(qreal x,qreal y,qreal w,qreal h);
~EllipseItem();
protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private:
QPointF posi;
QPointF endi;
QList<LineItem *> arrows;
};

ellipseitem.cpp


EllipseItem::EllipseItem(qreal x,qreal y,qreal w,qreal h)
: QGraphicsEllipseItem(x,y,w,h) {}

EllipseItem::~EllipseItem() {}

void EllipseItem::mouseMoveEvent(QGraphicsSceneMouseEve nt *e)
{
posi = e->scenePos();
setRect(posi.x()-5, posi.y()-5, 10, 10);
endi=e->scenePos();
setRect(endi.x()-5, endi.y()-5, 10, 10);
}

void EllipseItem::mouseReleaseEvent(QGraphicsSceneMouse Event *e)
{
endi=e->scenePos();

if(scene()->itemAt(endi))
{ LineItem *linie = new LineItem(posi.x(), posi.y(), endi.x(), endi.y());
scene()->addItem(linie);
}
else {QGraphicsEllipseItem::mouseReleaseEvent(e);}
}

QVariant EllipseItem::itemChange(GraphicsItemChange change, const QVariant &value)
{

if (change == QGraphicsItem::ItemPositionChange)
{
foreach (LineItem *arrow, arrows)
{
arrow->updatePosition();
}
}
return value;
}

lineitem.cpp


LineItem::LineItem(qreal x1, qreal y1, qreal x2, qreal y2)
: QGraphicsLineItem(x1, y1, x2, y2) {}

LineItem::~LineItem() {}

void LineItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
{ startpkt = e->scenePos(); }

void LineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{
endpkt = e->scenePos();
if(scene()->itemAt(endpkt))
{ setLine(QLineF(startpkt.x(), startpkt.y(), endpkt.x(), endpkt.y())); }
}

void LineItem::updatePosition()
{ QLineF line(startpkt.x(), startpkt.y(), endpkt.x(), endpkt.y());
setLine(line);
}

void LineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *)
{
EllipseItem *myStartItem = new EllipseItem(startpkt.x(), startpkt.y(),10,10);
EllipseItem *myEndItem = new EllipseItem(endpkt.x(), endpkt.y(),10,10);

if (myStartItem->collidesWithItem(myEndItem))
return;
else
{ painter->drawLine(QLineF(startpkt.x(), startpkt.y(), endpkt.x(), endpkt.y())); }

}

Can you see the mistake?