Free hand drawing is usually done by drawing a series of lines with the positions gathered from the mouse movements.
Cheers,
_
Free hand drawing is usually done by drawing a series of lines with the positions gathered from the mouse movements.
Cheers,
_
Vivek1982 (18th September 2014)
For drawing arrows, you can refer to elasticnodes example in Qt.
![]()
Vivek1982 (18th September 2014)
How can you draw anything on QGraphicsScene using QPainter? QPainter can be used only to draw on objects based on QPaintDevice. Do you mean QGraphicsView maybe?
The problem with freehand drawing using the mouse is that the lines look very bad. Every tiny movement of the mouse will add a new point to the list, and the result will be a jagged and wiggling line.
There are several things you can try to fix this problem:
- Save only every "nth" point (maybe one in every ten)
- Save a point only when it is greater than "n" pixels away from the last one.
- Save all the points, but then smooth the line afterward.
- Convert the freehand line into a Bezier curve
- Don't save every point - save only the start and end points, then let the user click the line to stretch it into a curve by adding control points
- Don't use freehand drawing - let the user click a series of points and draw a polyline between them
Smoothing can be done by a "running average". Take the first three points, compute the average or the x values and the average of the y values, and replace x[0] and y[0] with the average. Shift by one (average x[1] through x[3], etc.) and replace x[1] by the average, and so on.
Or, smooth by averaging and delete every other point afterwards.
You can turn the line into a Bezier curve by making every "nth" point a control point for the curve, then deleting the original points and replacing them with the Bezier curve. This would be easiest using the QEasingCurve class and QEasingCurve::toCubicSpline().
Vivek1982 (18th September 2014)
Thanks alot for the inputs. But actuall my task is like MS painting, i need to create a painting tool in Qt. In that application if i want to draw line on QGraphicVeiw. Mouse press and release events are used for drawing, I need to draw simultenously as on when i move the cursor after pressing mouse from start point (Similar to MS painting). The moment i release mouse event. to that point line to be drawn.
The same applies for Curve line also. If i start pressing mouse and during mousemove, the curve line to be drawn once i release the mouse. But Im not finding way for this.. Waiting for Suggestions to continue work![]()
I am not sure what you have problems with.
I assume you create an item when you start drawing, e.g. a QGraphicsLineItem, and then you update the item when the use moves the mouse, no?
Cheers,
_
No, Initially Im calling Event filter. code shown below. Im getting line,rect etc after i release mouse, not during the mouse move event.
Qt Code:
ui(new Ui::MainWindow) { ui->setupUi(this); ui->graphicsView->setScene(scene); scene->installEventFilter(this); //In the Switch cases Im calling for Mouse press, move and release events. Then update() func in move and release cases. The same graphicview co-ordinates are stored in Static variable, accesed same in the Paintevent. { qDebug("Entering the paint"); painter.setPen(myPen); scene->addRect(r1,myPen);To copy to clipboard, switch view to plain text mode
Well, if you don't want to use the items for drawing during creation, then you'll have to store the respective data and draw yourself.
Cheers,
_
Bookmarks