PDA

View Full Version : Curve Drawing



Vivek1982
17th September 2014, 12:26
Dear All,

I'm able to draw lines on QGraphicScene by using Qpainter. I need to draw Arrow pointed lines and curve line (Free hand drawing). I have used QMouseEvents like mousepress, mousemove and mouserelease to get positions. Drawing free hand or curve lines im finding it difficult. Suggest me to make it.

Thanks in advance

anda_skoa
17th September 2014, 13:44
Free hand drawing is usually done by drawing a series of lines with the positions gathered from the mouse movements.

Cheers,
_

aamer4yu
17th September 2014, 16:11
For drawing arrows, you can refer to elasticnodes example in Qt.

:)

d_stranz
17th September 2014, 18:40
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
24th September 2014, 13:24
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:)

anda_skoa
24th September 2014, 16:40
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,
_

Vivek1982
25th September 2014, 05:08
No, Initially Im calling Event filter. code shown below. Im getting line,rect etc after i release mouse, not during the mouse move event.



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene=new QGraphicsScene(0,0,740,400);
ui->graphicsView->setScene(scene);
scene->installEventFilter(this);

bool MainWindow::eventFilter(QObject* obj, QEvent* e )
//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.

void MainWindow::paintEvent(QPaintEvent *e)
{
qDebug("Entering the paint");
QPainter painter(this);
QPen myPen(Qt::blue, 3,Qt::SolidLine);
painter.setPen(myPen);
QRectF r1(s_count_x,s_count_y,(s_count_x1-s_count_x),(s_count_y1-s_count_y));
scene->addRect(r1,myPen);

anda_skoa
25th September 2014, 10:45
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,
_