Qt无限直线的绘制

Drawing of infinite line in Qt

我在 Qt 中绘图时遇到一些问题。

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

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

但我没有找到任何解决方案或接近我的问题的东西。 我希望有人遇到类似的问题并能帮助我。 预先感谢您的所有建议。

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

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

之后,您可以使用 QLineF 创建您的 "infinite" 行。

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))