Qt中如何用鼠标将直线变成曲线?

发布于 2024-11-15 09:22:31 字数 1196 浏览 0 评论 0原文

我的一个项目遇到了麻烦。我试图在渲染区域中绘制一条汽车路线(街道),其中可以包含直线和曲线。为此,我考虑主要绘制线条,然后用鼠标选择一条线,并通过移动鼠标将其转换为曲线(一条曲线,该曲线具有鼠标选择的线上的点)。到目前为止,我只能在渲染区域中绘制点并自动在这些点之间生成线条,但我不确定如何用鼠标将线条转换为曲线。

到目前为止我的代码是:

renderarea.cpp:


RenderArea::RenderArea(QWidget *parent)
    : QWidget(parent)
{
    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);
}
void RenderArea::mousePressEvent(QMouseEvent *e)
{
    point = e->pos();
    updateList(point);
    this->update();
}
void RenderArea::updateList(const QPoint& p)
{
    Point point;
    point.point = p;
    list.append(point);
    if (list.count()>1)
        lineAdded(point);
}
void RenderArea::lineAdded(const Point &p)
{
    Line temp;
    temp.endPoint = p;
    temp.startPoint = list.at(list.count() - 2);
    lines.append(temp);
}
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
    int i;
    QPainter painter(this);
    painter.setPen(QPen(Qt::black,2));
    for (i = 0; i < list.size(); ++i)
        painter.drawPoint(list[i].point);
    for (i = 0; i < lines.size(); ++i)
        painter.drawLine(lines[i].startPoint.point, lines[i].endPoint.point);
}

希望你能帮助我。提前致谢。

I am having trouble with a project of mine. I am trying to draw in an render-area a course for the cars (a street) which can contain both straight lines and curves. For that I was thinking of primarily drawing the lines and then with the mouse selecting one line and transforming it into a curve by moving the mouse (a curve which has as peek the point on the line selected by the mouse). Until now I only managed to draw points in the render area and automatically generate lines between these points, but I am not sure about how to transform the line into a curve with the mouse.

My code until now is:

renderarea.cpp:


RenderArea::RenderArea(QWidget *parent)
    : QWidget(parent)
{
    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);
}
void RenderArea::mousePressEvent(QMouseEvent *e)
{
    point = e->pos();
    updateList(point);
    this->update();
}
void RenderArea::updateList(const QPoint& p)
{
    Point point;
    point.point = p;
    list.append(point);
    if (list.count()>1)
        lineAdded(point);
}
void RenderArea::lineAdded(const Point &p)
{
    Line temp;
    temp.endPoint = p;
    temp.startPoint = list.at(list.count() - 2);
    lines.append(temp);
}
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
    int i;
    QPainter painter(this);
    painter.setPen(QPen(Qt::black,2));
    for (i = 0; i < list.size(); ++i)
        painter.drawPoint(list[i].point);
    for (i = 0; i < lines.size(); ++i)
        painter.drawLine(lines[i].startPoint.point, lines[i].endPoint.point);
}

Hope you can help me. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-11-22 09:22:31

有一些 UI(右键单击?)将线段更改为贝塞尔曲线。然后通过拖动手柄(需要提供)来控制曲线的形状。再次右键单击将曲线更改回线段。

Have some UI (right click?) that changes the line segment to Besier curve. Then control the shape of the curve by dragging handles (which you will need to provide). Another right click changes the curve back to segment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文