Results 1 to 9 of 9

Thread: connecting lines?

  1. #1

    Default connecting lines?

    Hi, I can draw a line between two points and if the point moves the line moves too.
    But now I would like to draw a second line between the first point and another one. This works, but then the line isn't connected to the points.
    I don't know what's wrong, can somebody help me?
    Qt Code:
    1. void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *e)
    2. {
    3. pos=e->scenePos();
    4. end=e->scenePos();
    5. item = new EllipseItem(pos.x(), pos.y(),10,10);
    6. item2 = new EllipseItem(end.x(), end.y(),10,10);
    7. item->setFlags(QGraphicsItem::ItemIsMovable);
    8. item2->setFlags(QGraphicsItem::ItemIsMovable);
    9.  
    10. if(e->button() == Qt::LeftButton)
    11. {
    12. if(itemAt(pos))
    13. {QGraphicsScene::mousePressEvent(e); }
    14.  
    15. else
    16. {addItem(item);
    17. linie = new LineItem(item,item2);
    18. QObject::connect(item, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    19. QObject::connect(item2, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    20. addItem(linie);
    21. }
    22. }
    23.  
    24. if(e->button() == Qt::RightButton)
    25. {
    26. if(itemAt(pos))
    27. {
    28. linie = new LineItem(item,item2);
    29. QObject::connect(item, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    30. QObject::connect(item2, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    31. addItem(linie);
    32. }
    33. else
    34. { QGraphicsScene::mousePressEvent(e); }
    35. }
    36. }
    37.  
    38. void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    39. {
    40. if((e->buttons()==Qt::LeftButton) || (e->buttons()==Qt::RightButton))
    41. {item2->setPos(e->scenePos());
    42. item2->update();
    43. linie->updatePosition();
    44. linie->update();
    45. }
    46. QGraphicsScene::mouseMoveEvent(e);
    47. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by konvex; 14th December 2008 at 14:45.

  2. #2

    Default Re: connecting lines?

    No idea whats wrong?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connecting lines?

    Could you prepare a minimal compilable example reproducing the problem?

  4. #4

    Default Re: connecting lines?

    Qt Code:
    1. void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *e)
    2. {
    3. pos=e->scenePos();
    4. end=e->scenePos();
    5. item = new EllipseItem(pos.x(), pos.y(),10,10);
    6. item2 = new EllipseItem(end.x(), end.y(),10,10);
    7. item->setFlags(QGraphicsItem::ItemIsMovable);
    8. item2->setFlags(QGraphicsItem::ItemIsMovable);
    9.  
    10. if(e->button() == Qt::LeftButton)
    11. {
    12. if(itemAt(pos))
    13. {QGraphicsScene::mousePressEvent(e); }
    14.  
    15. else
    16. {
    17. addItem(item);
    18. linie = new LineItem(item,item2);
    19. QObject::connect(item, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    20. QObject::connect(item2, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    21. addItem(linie);
    22. //here I draw the item and a line, which is connected to the item and item2, this works
    23. }
    24. }
    25.  
    26. if(e->button() == Qt::RightButton)
    27. {
    28. if(itemAt(pos))
    29. {
    30. linie = new LineItem(item,item2);
    31. QObject::connect(item, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    32. QObject::connect(item2, SIGNAL(positionchanged()), linie , SLOT(updatePosition()));
    33. addItem(linie);
    34. //here should appear the second line (without creating a new item(startpoint) and a new item2, which is connected, too.
    35. //but when I move item or item2 , the line doesn't move
    36. //this doesn't work
    37. }
    38. else
    39. { QGraphicsScene::mousePressEvent(e); }
    40. }
    41. }
    42.  
    43. void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    44. {
    45. if((e->buttons()==Qt::LeftButton) || (e->buttons()==Qt::RightButton))
    46. {item2->setPos(e->scenePos());
    47. item2->update();
    48. linie->updatePosition();
    49. linie->update();
    50. }
    51. QGraphicsScene::mouseMoveEvent(e);
    52. }
    53.  
    54. void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    55. {
    56. QGraphicsScene::mouseReleaseEvent(e);
    57. end=e->scenePos();
    58.  
    59. if(itemAt(end))
    60. {QGraphicsScene::mouseReleaseEvent(e);}
    61. else
    62. {addItem(item2);} //here I draw item2
    63. }
    To copy to clipboard, switch view to plain text mode 
    Do you know how I could connect the item to the line?
    Last edited by konvex; 15th December 2008 at 12:34.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connecting lines?

    This is not a compilable example reproducing the problem. It's two methods of some nondefined class. I was asking for something I can compile (qmake && make) and run to see the problem myself.

  6. #6

    Default

    sorry, here is the first part

    and here th second

    Edit (wysota): - next time please attach an archive instead of standalone files
    Attached Files Attached Files
    Last edited by wysota; 16th December 2008 at 01:06. Reason: Cleanup

  7. #7

    Default Re: connecting lines?

    can you see what's missing ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connecting lines?

    The problem is in your release event:
    Qt Code:
    1. void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    2.  
    3. {
    4.  
    5. QGraphicsScene::mouseReleaseEvent(e);
    6. end=e->scenePos();
    7.  
    8. if(itemAt(end))
    9. {
    10. QGraphicsScene::mouseReleaseEvent(e);
    11.  
    12. }
    13.  
    14. else
    15. {
    16. addItem(item2);
    17.  
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    If you are releasing the mouse on the first line, itemAt() returns a valid pointer, thus an item is not added (the base implementation is called instead).

    In general we'd do it differently. Either create a single item consisting of a line with two ellipses or create two ellipses and use QGraphicsItem::itemChange() to react on updates of items' geometry to snap the lines connecting the ellipses into place.

    Something like this:
    Qt Code:
    1. QVariant itemChange ( GraphicsItemChange change, const QVariant & value ) {
    2. if (change==QGraphicsItem::ItemPositionHasChanged) {
    3. QLineF li = lineItem->line();
    4. if(isStartPoint()) li.setP1(pos());
    5. else li.setP2(pos());
    6. lineItem->setLine(li);
    7. }
    8. return QGraphicsEllipseItem::itemChange(change, value);
    9. }
    To copy to clipboard, switch view to plain text mode 

  9. #9

    Default Re: connecting lines?

    Thanks, but can you help me to realize?!
    Qt Code:
    1. QVariant itemChange ( GraphicsItemChange change, const QVariant & value ) {
    2. if (change==QGraphicsItem::ItemPositionHasChanged) {
    3. QLineF li = lineItem->line();
    4. //the class LineItem is LineItem(item1,item2)
    5. //so I have to write something like QLineF li = lineItem(item1,item2)->line();
    6. //but in the class ellipseitem I can't creat two ellipses?!
    7. if(isStartPoint()) li.setP1(pos());
    8. else li.setP2(pos());
    9. lineItem->setLine(li);
    10. }
    11. return QGraphicsEllipseItem::itemChange(change, value);
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 12
    Last Post: 18th September 2008, 16:04
  2. Connect to a server, send data and exit
    By Pepe in forum Newbie
    Replies: 6
    Last Post: 27th July 2007, 12:29
  3. How to draw lines with the right width?
    By albanelporto in forum Qt Programming
    Replies: 3
    Last Post: 22nd November 2006, 12:51
  4. Replies: 5
    Last Post: 28th August 2006, 15:36

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.