In my scene I only make an ellipse that is movable when I use the mouse.
ellipseitem.h
{
public:
EllipseItem(qreal x,qreal y,qreal w,qreal h);
~EllipseItem();
protected:
private:
QList<LineItem *> arrows;
};
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;
};
To copy to clipboard, switch view to plain text mode
ellipseitem.cpp
EllipseItem::EllipseItem(qreal x,qreal y,qreal w,qreal h)
EllipseItem::~EllipseItem() {}
{
posi = e->scenePos();
setRect(posi.x()-5, posi.y()-5, 10, 10);
endi=e->scenePos();
setRect(endi.x()-5, endi.y()-5, 10, 10);
}
{
endi=e->scenePos();
if(scene()->itemAt(endi))
{ LineItem *linie = new LineItem(posi.x(), posi.y(), endi.x(), endi.y());
scene()->addItem(linie);
}
}
QVariant EllipseItem
::itemChange(GraphicsItemChange change,
const QVariant &value
) {
{
foreach (LineItem *arrow, arrows)
{
arrow->updatePosition();
}
}
return value;
}
EllipseItem::EllipseItem(qreal x,qreal y,qreal w,qreal h)
: QGraphicsEllipseItem(x,y,w,h) {}
EllipseItem::~EllipseItem() {}
void EllipseItem::mouseMoveEvent(QGraphicsSceneMouseEvent *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(QGraphicsSceneMouseEvent *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;
}
To copy to clipboard, switch view to plain text mode
lineitem.cpp
LineItem::LineItem(qreal x1, qreal y1, qreal x2, qreal y2)
LineItem::~LineItem() {}
{ startpkt = e->scenePos(); }
{
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);
}
{
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()));
}
}
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())); }
}
To copy to clipboard, switch view to plain text mode
Can you see the mistake?
Bookmarks