Qt中如何用鼠标将直线变成曲线?
我的一个项目遇到了麻烦。我试图在渲染区域中绘制一条汽车路线(街道),其中可以包含直线和曲线。为此,我考虑主要绘制线条,然后用鼠标选择一条线,并通过移动鼠标将其转换为曲线(一条曲线,该曲线具有鼠标选择的线上的点)。到目前为止,我只能在渲染区域中绘制点并自动在这些点之间生成线条,但我不确定如何用鼠标将线条转换为曲线。
到目前为止我的代码是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些 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.