Qtで曲線を描く必要があります:ユーザーが(QGraphicsViewを介して)QGraphicsSceneをクリックすると、ユーザーがクリックしたポイント間に直線が描画されます。ユーザーが(右ボタンをクリックして)直線を描き終えると、線のセットは曲線になります。
そのためには、QPainterPath::cubicTo(...)
メソッドを使用し、を使用してQGraphicsSceneへのパスを追加する必要がありますQGraphicsScene::addPath(...)
。
問題は、に渡されるパラメータ値を計算する方法がわからないことcubicTo(...)
です。
たとえば、次の図では、ユーザーは点ABとCをクリックして2本の灰色の線を描画しています。右ボタンをクリックすると、次のコマンドを使用して赤い線を描画します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;
}
}
}