PDA

View Full Version : Line in GraphicsScene



konvex
12th November 2008, 11:59
Hi,
I wrote a little program, which draws a ellipse(in the scene) when I click. Now I want to draw a line if I click on the ellipse and pull(then should appear a new ellipse with the line between them).
But I have no idea how to realize..
I hope somebody can give an example or a tip.

graphicsscene.cpp


void GraphicsScene::mousePressEvent(QGraphicsSceneMouse Event *e)
{
pos=e->buttonDownScenePos(Qt::LeftButton);
}

void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMou seEvent *e)
{
pos=e->buttonDownScenePos(Qt::LeftButton);
addEllipse (pos.x()-4, pos.y()-4, 8, 8);
}

aamer4yu
12th November 2008, 12:04
Well, firstly, you need to inform the scene that the ellipse has been clicked.
This can be done in 2 ways - 1) subclass ellipse item and emit a signal on mouse press (Note: graphics item donot have signal slots, they arent inherited by QObject)
2) in mousepress event of scene, check the item under mouse and see if its the ellipse item you want.

Second step is do some action on ellipse clicked -
you have the first ellipse position, now based on that, add second ellipse item and line.

Thirdly, you need to join line between these 2 ellipse items. For this you can have a look at the diagram scene example in Qt Demos


Hope this helps :)

konvex
12th November 2008, 13:59
I have created a subclass EllipseItem and now first I tried to show it in the scene, but the scene is empty!? I don't understand this...

graphicsscene.cpp


GraphicsScene::GraphicsScene()
{
EllipseItem item(100,100,10,10);
addItem(&item);
}


ellipse.h


class EllipseItem : public QGraphicsEllipseItem
{
public:
EllipseItem(int x,int y,int w,int h) : QGraphicsEllipseItem (x,y,w,h) {}
};


and when i try without creating a subclass graphicsscene it shows the ellipse.

aamer4yu
13th November 2008, 05:25
Your ellipse item is getting created on stack of the graphics scene. Hence it gets destroyed after the ctor.

Use heap for the ellipse item

konvex
16th November 2008, 13:14
Now I have done something like this, but it does not work.
graphicsscene.cpp


void GraphicsScene::mousePressEvent(QGraphicsSceneMouse Event *e)
{
pos=e->scenePos();
if(e->button() == Qt::LeftButton)
{
if(itemAt(pos))
{ QGraphicsScene::mousePressEvent(e); }
else
{
EllipseItem *item = new EllipseItem(pos.x()-5, pos.y()-5,10,10);
item->setFlags(QGraphicsItem::ItemIsMovable);
addItem(item);
}}}

ellipseitem.cpp


void EllipseItem::mouseMoveEvent(QGraphicsSceneMouseEve nt *e)
{
setRect((e->scenePos().x()-5),(e->scenePos().y()-5),10,10);
end = e->scenePos();
setFlag(QGraphicsItem::ItemIsSelectable, true);

if(isSelected() && e->button() == Qt::RightButton)
{
sc = new QGraphicsScene;
LineItem *linie = new LineItem((e->scenePos().x()),(e->scenePos().y()),end.x(),end.y());
sc->addItem(linie);
}}

lineitem.cpp


void LineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{
startpkt = e->scenePos();
endpkt = e->scenePos();
setLine(startpkt.x(),startpkt.y(),endpkt.x(),endpk t.y());
}

I draw two ellipse (this works) in the scene and if I choose one and move the mouse to the other the should appear the line(this don't work).
I don't know whats the problem...
Please help

aamer4yu
16th November 2008, 13:36
From an earlier post

Thirdly, you need to join line between these 2 ellipse items. For this you can have a look at the diagram scene example in Qt Demos

Did you see that demo example ??

konvex
16th November 2008, 13:53
Yes, I looked at the example, but although I don't know how to do it...
The scene don't react if I use the right button.