【发布时间】:2012-10-19 18:35:20
【问题描述】:
我需要用 Qt 绘制曲线:用户点击 QGraphicsScene(通过 QGraphicsView)并在用户点击的点之间绘制直线。当用户画完直线(通过点击右键),这组线变成了一条曲线。
为此,我需要使用QPainterPath::cubicTo(...) 方法并使用QGraphicsScene::addPath(...) 将路径添加到QGraphicsScene。
问题是我不知道如何计算传递给cubicTo(...) 的参数值。
例如下图中,用户通过点击A点B和C点绘制了两条灰线。当她点击右键时,我想使用cubicTo(...)绘制红线:
我当前的代码只画了灰线,因为我已将c1、c2、d1 和d2 值设置为用户点击的点位置:
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;
}
}
}
【问题讨论】:
标签: qt curve-fitting qgraphicsscene qpainter mousepress