I need to draw curves with Qt: The user clicks on QGraphicsScene (via QGraphicsView) and straight lines are drawn between the points clicked on by the user. When the user finishes to draw straight lines (by clicking on the right button), the set of lines becomes a curve.
For that, I need to use the QPainterPath::cubicTo(...)
method and add the path to the QGraphicsScene using QGraphicsScene::addPath(...)
.
The problem is that I don't know how to compute the parameter values that are passed to cubicTo(...)
.
For example, in the following picture, the user has drawn the two gray lines by clicking points A B and C. When she clicks the right button, I want to draw the red line using cubicTo(...)
:
My current code only draws gray lines because I have set c1
, c2
, d1
and d2
values to point positions clicked on by the user :
void Visuel::mousePressEvent(QMouseEvent *event)
{
int x = ((float)event->pos().x()/(float)this->rect().width())*(float)scene()->sceneRect().width();
int y = ((float)event->pos().y()/(float)this->rect().height())*(float)scene()->sceneRect().height();
qDebug() << x << y;
if(event->button() == Qt::LeftButton)
{
path->cubicTo(x,y,x,y,x,y);
}
if(event->button() == Qt::RightButton)
{
if(path == NULL)
{
path = new QPainterPath();
path->moveTo(x,y);
}
else
{
path->cubicTo(x,y,x,y,x,y);
scene()->addPath(*path,QPen(QColor(79, 106, 25)));
path = NULL;
}
}
}