2024-09-28 16:22:14 发布
网友
有没有一种方法可以用多个点来画一条线,然后通过某种鼠标悬停事件来捕捉这条线
不幸的是,我很难在一个有多个中间点的QGraphicsView中画一条线
我知道如何用QtWidgets.QGraphicsPolygonItem(QPolygonF...)绘制闭合多边形。 如果我的多边形点未闭合-这意味着最后一个点不等于第一个点-多边形将自动闭合
QtWidgets.QGraphicsPolygonItem(QPolygonF...)
但是我不想有最后的连接
使用QtWidgets.QGraphicsLineItem只能在两点之间绘制一条线
QtWidgets.QGraphicsLineItem
一种可能的解决方案是使用QPainterPath:
import random from PyQt5 import QtGui, QtWidgets class GraphicsPathItem(QtWidgets.QGraphicsPathItem): def mousePressEvent(self, event): super().mousePressEvent(event) print("Local position:", event.pos()) print("Scene position:", event.scenePos()) def shape(self): if self.path() == QtGui.QPainterPath(): return self.path() pen = self.pen() ps = QtGui.QPainterPathStroker() ps.setCapStyle(pen.capStyle()) width = 2 * max(0.00000001, pen.widthF()) ps.setWidth(width) ps.setJoinStyle(pen.joinStyle()) ps.setMiterLimit(pen.miterLimit()) return ps.createStroke(path) app = QtWidgets.QApplication([]) scene = QtWidgets.QGraphicsScene() view = QtWidgets.QGraphicsView(scene) view.setRenderHint(QtGui.QPainter.Antialiasing) view.resize(640, 480) view.show() path_item = GraphicsPathItem() path_item.setPen(QtGui.QPen(QtGui.QColor("red"), 5)) path = QtGui.QPainterPath() path.moveTo(0, 0) for i in range(5): x, y = random.sample(range(300), 2) path.lineTo(x, y) path_item.setPath(path) scene.addItem(path_item) app.exec_()
我接受了这个想法并使用了以下代码:
path = QtGui.QPainterPath() path.addPolygon(polyline) new_item = QtWidgets.QGraphicsPathItem(path, None) new_item.setPath(path) scene.addItem(new_item)
在polyline中,我已经有一个QPolygonF对象保存所有点
polyline
一种可能的解决方案是使用QPainterPath:
我接受了这个想法并使用了以下代码:
在
polyline
中,我已经有一个QPolygonF对象保存所有点相关问题 更多 >
编程相关推荐