PDA

View Full Version : Live preview for graphics item in Qt



Kamalpreet
8th November 2014, 07:59
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.


lineItem->setLine(start_p.x(), start_p.y(), move_p.x(), move_p.y());
mUndoStack->push(new CadCommandAdd(this, lineItem));

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:


virtual void undo()
{
m_scene->removeItem(m_item); // m_scene is the QGraphicsScene, m_item is the QGraphicsItem
}

virtual void redo()
{
m_scene->addItem(m_item);
}


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:

10739

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

anda_skoa
8th November 2014, 09:39
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?


I am afraid I don't understand the problem.

It sounds like you are doing something like creating CadCommandAdd instances for every mouse move instead of just when you finish the line.
If that is the case just don't do that.

Cheers,
_

Kamalpreet
9th November 2014, 14:25
It sounds like you are doing something like creating CadCommandAdd instances for every mouse move instead of just when you finish the line.
If that is the case just don't do that.


I have created an instance of CadCommandAdd only in mousePressEvent of the graphics scene when mouse is pressed for the first time when it is in graphics scene.
While mouse is moved, only the updated position is obtained. No instances of CadCommandAdd are created while mouseMoveEvent.

anda_skoa
10th November 2014, 07:54
In that case my guess would be too aggressive caching.

Cheers,
_