在Qt中绘制无限线

问题描述 投票:0回答:1

我在Qt中画画时遇到了一些问题。

我需要用QPainter在QGraphicsScene上绘制一条无限的线。而关于线我只知道基点和线方向(或基点和另一个点,在这条线上)。

结果,我需要这样的东西。

但我没有找到任何解决方案或与我的问题接近的东西。我希望有人遇到类似的问题,可以帮助我。提前感谢您的所有建议。

c++ qt drawing qgraphicsscene qpainter
1个回答
0
投票

您可以假设无限线是起点和终点在场景之外的线。

如果计算场景的对角线长度,则任何线条的最大可见长度。

之后,您可以使用QLineF创建“无限”线。

PyQt5的一个例子:

direction = -45
basePoint = QPointF(200, 200)

maxLength = math.sqrt(scene.width() ** 2 * scene.height() ** 2)

line1 = QLineF(basePoint, basePoint + QPointF(1, 0)) # Avoid an invalid line
line2 = QLineF(basePoint, basePoint + QPointF(1, 0))

# Find the first point outside the scene
line1.setLength(maxLength / 2)
line1.setAngle(direction)

# Find the sceond point outside the scene
line2.setLength(maxLength / 2)
line2.setAngle(direction + 180)

# Make a new line with the two end points
line = QLineF(line1.p2(), line2.p2())

scene.addItem(QGraphicsLineItem(line))
© www.soinside.com 2019 - 2024. All rights reserved.