I am adding entities to QGraphicsScene by creating commands in QUndoFramework. An entity is pushed to the QUndoStack so that it is added to the scene.

Qt Code:
  1. lineItem->setLine(start_p.x(), start_p.y(), move_p.x(), move_p.y());
  2. mUndoStack->push(new CadCommandAdd(this, lineItem));
To copy to clipboard, switch view to plain text mode 

where CadCommand is the command class to add entities and lineItem is the line added as an entity to the scene.

The CadCommand has the undo and redo functions defined as:
Qt Code:
  1. virtual void undo()
  2. {
  3. m_scene->removeItem(m_item); // m_scene is the QGraphicsScene, m_item is the QGraphicsItem
  4. }
  5.  
  6. virtual void redo()
  7. {
  8. m_scene->addItem(m_item);
  9. }
To copy to clipboard, switch view to plain text mode 

I am trying to achieve live preview of line until the mouse is clicked for second point to get its end point. The mousePressEvent is used to get the start and end points and mouseMoveEvent is used to get the updated coordinates of line's end point and an updated view is generated like:

screenshot from 2014-11-07 15:40:21.png

I want to add only the line joining First Click and Second Click to the scene. The other lines are such that the end point is the mouse position when mouse is moved in the scene. I want this to provide a live preview for the line and not that it should be added permanently in the drawing area.

How to achieve a line having First Click and Second Click as end points only? And how to delete the previous lines which show the preview while the mouse is moved?

Note: This is a cross question: http://stackoverflow.com/q/26799255/2828747