PDA

View Full Version : Something like a line rubber band but that can be diagonal?



feraudyh
16th June 2010, 21:05
Hello,
I want to do a graphic program with the possibility of drawing a straight line segment by clicking on a point that represents the start, dragging the mouse and releasing it at the other end. Then a line is drawn. So far , I can program this, but to make it super user friendly I would like the look and feel to be like stretching a rubber band, in other words there should be a
line from the cursor to the start point that is continuously redrawn as the mouse is moved, and that involves erasing the old
line. All of this needs to be done without flicker. Any suggestions?
It seems that QrubberBand can draw lines, but they have to be horizontal or vertical.

Oh, I am aware that there is a graphics scene demo that provides something like this, but I have been avoiding graphics scenes so far because I am worried that the background scene which has thousands of static objects (it's a map of a town with thousands of buildings) might overload the graphics scene. Is my fear justified?

Thankyou
Henri

aamer4yu
17th June 2010, 06:18
You can keep a track of current mouse move position in a member variable.
On mouse click set drawLine = true.

Then in paintEvent,

if(drawLine)
{
// draw line from mousePressPoint to current mouse move point...
}

That should work..

feraudyh
17th June 2010, 13:38
Thanykou for the suggestion which I have successfully implemented.
I'd just like to add two remarks:
Since my appllication allows for zooming and moving I have to take the viewport into account by converting mouse coordinates (event->pos()) to real world coordinates which I pass on to the line drawing function.
In the method MouseMoveEvent the update of the coordinates corresponding to the mouse position needs to be followied by an update() or else the Painter event is is not launched.
Despite this I see no flicker related to redrawing the map beneath, and this comes as a surprise.Maybe it's because it's so damn fast.