Results 1 to 7 of 7

Thread: Line in GraphicsScene

  1. #1

    Default Line in GraphicsScene

    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
    Qt Code:
    1. void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *e)
    2. {
    3. pos=e->buttonDownScenePos(Qt::LeftButton);
    4. }
    5.  
    6. void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    7. {
    8. pos=e->buttonDownScenePos(Qt::LeftButton);
    9. addEllipse (pos.x()-4, pos.y()-4, 8, 8);
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Line in GraphicsScene

    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

  3. #3

    Default Re: Line in GraphicsScene

    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
    Qt Code:
    1. GraphicsScene::GraphicsScene()
    2. {
    3. EllipseItem item(100,100,10,10);
    4. addItem(&item);
    5. }
    To copy to clipboard, switch view to plain text mode 

    ellipse.h
    Qt Code:
    1. class EllipseItem : public QGraphicsEllipseItem
    2. {
    3. public:
    4. EllipseItem(int x,int y,int w,int h) : QGraphicsEllipseItem (x,y,w,h) {}
    5. };
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Line in GraphicsScene

    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

  5. #5

    Default Re: Line in GraphicsScene

    Now I have done something like this, but it does not work.
    graphicsscene.cpp
    Qt Code:
    1. void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *e)
    2. {
    3. pos=e->scenePos();
    4. if(e->button() == Qt::LeftButton)
    5. {
    6. if(itemAt(pos))
    7. { QGraphicsScene::mousePressEvent(e); }
    8. else
    9. {
    10. EllipseItem *item = new EllipseItem(pos.x()-5, pos.y()-5,10,10);
    11. item->setFlags(QGraphicsItem::ItemIsMovable);
    12. addItem(item);
    13. }}}
    To copy to clipboard, switch view to plain text mode 
    ellipseitem.cpp
    Qt Code:
    1. void EllipseItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    2. {
    3. setRect((e->scenePos().x()-5),(e->scenePos().y()-5),10,10);
    4. end = e->scenePos();
    5. setFlag(QGraphicsItem::ItemIsSelectable, true);
    6.  
    7. if(isSelected() && e->button() == Qt::RightButton)
    8. {
    9. sc = new QGraphicsScene;
    10. LineItem *linie = new LineItem((e->scenePos().x()),(e->scenePos().y()),end.x(),end.y());
    11. sc->addItem(linie);
    12. }}
    To copy to clipboard, switch view to plain text mode 
    lineitem.cpp
    Qt Code:
    1. void LineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    2. {
    3. startpkt = e->scenePos();
    4. endpkt = e->scenePos();
    5. setLine(startpkt.x(),startpkt.y(),endpkt.x(),endpkt.y());
    6. }
    To copy to clipboard, switch view to plain text mode 
    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

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Line in GraphicsScene

    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 ??

  7. #7

    Default Re: Line in GraphicsScene

    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.

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.