我试图用两种方式画一条由线组成的路径: 首先,路径是由直线组成的,它们的端点是相同的。使用此方法的问题在于,这些线在其末端重叠,造成不良影响,如下图所示:
代码如下:
from PyQt5 import QtWidgets, QtCore, QtGui
import typing
import random
class Track(QtWidgets.QGraphicsPathItem):
def __init__(self, parent=None, offset: float = 50):
super(Track, self).__init__(parent) # initiate the parent class
self.__points: list = [QtCore.QPointF(0, 0)]
self.__pen: QtGui.QPen = QtGui.QPen()
self.setPen()
def getPoints(self) -> list:
return self.__points
def append(self, point: QtCore.QPointF):
self.__points.append(point)
def getPen(self) -> QtGui.QPen:
return self.__pen
def setPen(self, pen: QtGui.QPen = None, width: int = 10, color: QtGui.QColor = QtGui.QColor(0, 24, 128, 100),
cap: QtCore.Qt.PenCapStyle = QtCore.Qt.SquareCap, line_style: QtCore.Qt.PenStyle = QtCore.Qt.SolidLine,
join: QtCore.Qt.PenJoinStyle = QtCore.Qt.RoundJoin) -> None:
"""
Set the pen that will be used to paint the implement.
:param pen : set the pen or its arguments
:param width: the pen width.
:param color: the pen color.
:param cap: the cap style: rounded, flatted and squared.
:param line_style: dashed, solid ...
:param join: miter , rounded ...
:return: None
"""
if pen == None:
self.__pen.setWidth(width) # set the pen width
self.__pen.setColor(color) # define your color from QtCore, it is safer to use the statement:
self.__pen.setCapStyle(cap) # set the cap style of the line
self.__pen.setStyle(line_style) # set the line style for instance: solid, dash... whatever
self.__pen.setJoinStyle(join) # set how the lines will be connected.
else:
self.__pen = pen
def paint(self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem,
widget: typing.Optional[QtWidgets.QWidget] = ...) -> None:
painter.setPen(self.getPen())
painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
try:
path = QtGui.QPainterPath()
path.moveTo(self.getPoints()[0])
for point in self.getPoints():
path.lineTo(point)
painter.drawPath(path)
except IndexError:
self.append(QtCore.QPointF(0, 0))
class Producer(QtCore.QObject):
def __init__(self, parent=None):
super(Producer, self).__init__(parent)
self.__last_point = QtCore.QPointF(10, 0)
self.__point = QtCore.QPointF(10, 0)
self.upper = 10
self.bottom = 0
def setPoint(self) -> None:
self.setLastpoint(self.getPoint())
x = random.randint(self.bottom, self.upper)
y = random.randint(self.bottom, self.upper)
self.upper += 50 # increases the range of probability at the upper limit
self.__point = QtCore.QPointF(x, y) # produce a new random point
def getPoint(self) -> QtCore.QPointF:
return self.__point
def setLastpoint(self, point: QtCore.QPointF):
self.__last_point = point
def getLastPoint(self) -> QtCore.QPointF:
return self.__last_point
class Window2(QtWidgets.QMainWindow):
def __init__(self):
super(Window2, self).__init__()
central_widget = QtWidgets.QWidget()
self.__pen = QtGui.QPen()
self.setMinimumHeight(500)
self.setMinimumWidth(500)
self.scene = QtWidgets.QGraphicsScene(self)
self.view = QtWidgets.QGraphicsView(self.scene)
self.view.setSceneRect(self.view.mapToScene(self.view.viewport().rect()).boundingRect())
self.btn = QtWidgets.QPushButton('Get Track')
self.btn.clicked.connect(self.getTrack)
self.producer = Producer()
hbox = QtWidgets.QHBoxLayout(central_widget)
hbox.addWidget(self.view)
hbox.addWidget(self.btn)
self.setCentralWidget(central_widget)
self.setPen()
def getPen(self) -> QtGui.QPen:
return self.__pen
def getTrack(self):
print('run')
self.producer.setPoint()
line = QtCore.QLineF(self.producer.getPoint(), self.producer.getLastPoint())
self.scene.addLine(line, pen = self.getPen())
dx = self.producer.getPoint().x() - self.producer.getLastPoint().x()
dy = self.producer.getPoint().y() - self.producer.getLastPoint().y()
print(dx, dy)
self.view.setSceneRect(self.view.sceneRect().translated(dx, dy))
def setPen(self, pen: QtGui.QPen = None, width: int = 10, color: QtGui.QColor = QtGui.QColor(0, 24, 128, 100),
cap: QtCore.Qt.PenCapStyle = QtCore.Qt.SquareCap, line_style: QtCore.Qt.PenStyle = QtCore.Qt.SolidLine,
join: QtCore.Qt.PenJoinStyle = QtCore.Qt.RoundJoin) -> None:
"""
Set the pen that will be used to paint the implement.
:param pen : set the pen or its arguments
:param width: the pen width.
:param color: the pen color.
:param cap: the cap style: rounded, flatted and squared.
:param line_style: dashed, solid ...
:param join: miter , rounded ...
:return: None
"""
if pen == None:
self.__pen.setWidth(width) # set the pen width
self.__pen.setColor(color) # define your color from QtCore, it is safer to use the statement:
self.__pen.setCapStyle(cap) # set the cap style of the line
self.__pen.setStyle(line_style) # set the line style for instance: solid, dash... whatever
self.__pen.setJoinStyle(join) # set how the lines will be connected.
else:
self.__pen = pen
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# w = Window()
w = Window2()
w.show()
sys.exit(app.exec_())
我使用的第二种方法是创建一条由点组成的连续路径。此路径继承类:QGraphicsPathItem。但是,当我“更新”我的“场景矩形”时,当它的一个端点在我的边界之外=“场景矩形”时,该路径将消失。有没有办法防止它消失? 我正在采取的这种方法的第二个问题是,我需要保存构成我的路径的这些点。。。对于少量的点,这不是问题,但当我的路径充满点时,它将在内存管理方面出现问题。图片:
代码如下:
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
central_widget = QtWidgets.QWidget()
self.setMinimumHeight(500)
self.setMinimumWidth(500)
self.scene = QtWidgets.QGraphicsScene(self)
self.view = QtWidgets.QGraphicsView(self.scene)
self.view.setSceneRect(self.view.mapToScene(self.view.viewport().rect()).boundingRect())
self.btn = QtWidgets.QPushButton('Get Track')
self.btn.clicked.connect(self.getTrack)
self.producer = Producer()
hbox = QtWidgets.QHBoxLayout(central_widget)
hbox.addWidget(self.view)
hbox.addWidget(self.btn)
self.track = Track()
self.scene.addItem(self.track)
self.setCentralWidget(central_widget)
def getTrack(self):
self.producer.setPoint()
self.track.append(self.producer.getPoint())
dx = self.producer.getPoint().x() - self.producer.getLastPoint().x()
dy = self.producer.getPoint().y() - self.producer.getLastPoint().y()
print(dx, dy)
self.view.setSceneRect(self.view.sceneRect().translated(dx, dy))
我要模拟一个GPS,我要画的路径是 汽车的排量。但我不知道这两种方法中哪一种更好,是否还有其他方法
这里是一个最小的可复制示例:
from PyQt5 import QtWidgets, QtCore, QtGui
import typing
import random
pen = QtGui.QPen()
pen.setColor(QtGui.QColor(0, 24, 128, 100))
pen.setWidth(10)
pen.setStyle(QtCore.Qt.SolidLine)
pen.setCapStyle(QtCore.Qt.SquareCap)
class Track(QtWidgets.QGraphicsPathItem):
def __init__(self, parent=None, offset: float = 50):
super(Track, self).__init__(parent) # initiate the parent class
self.__points: list = [QtCore.QPointF(0, 0)]
def getPoints(self) -> list:
return self.__points
def append(self, point: QtCore.QPointF):
self.__points.append(point)
def paint(self, painter: QtGui.QPainter, option: QtWidgets.QStyleOptionGraphicsItem,
widget: typing.Optional[QtWidgets.QWidget] = ...) -> None:
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
path = QtGui.QPainterPath()
path.moveTo(self.getPoints()[0])
for point in self.getPoints():
path.lineTo(point)
painter.drawPath(path)
class Producer(QtCore.QObject):
def __init__(self, parent=None):
super(Producer, self).__init__(parent)
self.__last_point = QtCore.QPointF(10, 0)
self.__point = QtCore.QPointF(10, 0)
self.upper = 10
def setPoint(self) -> None:
self.setLastpoint(self.getPoint())
x = random.randint(0, self.upper)
y = random.randint(0, self.upper)
self.upper += 50 # increases the range of probability at the upper limit
self.__point = QtCore.QPointF(x, y) # produce a new random point
def getPoint(self) -> QtCore.QPointF:
return self.__point
def setLastpoint(self, point: QtCore.QPointF):
self.__last_point = point
def getLastPoint(self) -> QtCore.QPointF:
return self.__last_point
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
central_widget = QtWidgets.QWidget()
self.setMinimumHeight(500)
self.setMinimumWidth(500)
self.scene = QtWidgets.QGraphicsScene(self)
self.view = QtWidgets.QGraphicsView(self.scene)
self.view.setSceneRect(self.view.mapToScene(self.view.viewport().rect()).boundingRect())
self.btn = QtWidgets.QPushButton('Get Track')
self.btn.clicked.connect(self.getTrack)
self.producer = Producer()
hbox = QtWidgets.QHBoxLayout(central_widget)
hbox.addWidget(self.view)
hbox.addWidget(self.btn)
self.track = Track()
self.scene.addItem(self.track)
self.setCentralWidget(central_widget)
def getTrack(self):
self.producer.setPoint()
self.track.append(self.producer.getPoint())
dx = self.producer.getPoint().x() - self.producer.getLastPoint().x()
dy = self.producer.getPoint().y() - self.producer.getLastPoint().y()
self.view.setSceneRect(self.view.sceneRect().translated(dx, dy))
class Window2(QtWidgets.QMainWindow):
def __init__(self):
super(Window2, self).__init__()
central_widget = QtWidgets.QWidget()
self.setMinimumHeight(500)
self.setMinimumWidth(500)
self.scene = QtWidgets.QGraphicsScene(self)
self.view = QtWidgets.QGraphicsView(self.scene)
self.view.setSceneRect(self.view.mapToScene(self.view.viewport().rect()).boundingRect())
self.btn = QtWidgets.QPushButton('Get Track')
self.btn.clicked.connect(self.getTrack)
self.producer = Producer()
hbox = QtWidgets.QHBoxLayout(central_widget)
hbox.addWidget(self.view)
hbox.addWidget(self.btn)
self.setCentralWidget(central_widget)
def getTrack(self):
self.producer.setPoint()
self.scene.addLine(QtCore.QLineF(self.producer.getPoint(), self.producer.getLastPoint()), pen=pen)
dx = self.producer.getPoint().x() - self.producer.getLastPoint().x()
dy = self.producer.getPoint().y() - self.producer.getLastPoint().y()
self.view.setSceneRect(self.view.sceneRect().translated(dx, dy))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# w = Window()
w = Window2()
w.show()
sys.exit(app.exec_())
使用多个线段显然不是正确的选择,因为直线被视为单个元素,如果没有折叠边,则无法绘制它们
QPainterPath方法最重要的问题是,当您使用QGraphicsPathItem时,您根本没有使用它,因为您覆盖了它的}
paint()
方法。手动绘制路径会使它完全无用,您应该使用它的^{
您的实现也是错误的,因为它不考虑路径的其余部分,并使用增量翻译。正如您在对另一个问题的评论中已经提到的,增量转换通常不是一个好主意,因为它们经常会产生误导,并且通常会导致意外行为(与本例完全相同)
解决方法是正确使用QGraphicsPathItem:
然后,为了确保项目始终可见,可以使用现有的^{} 或^{} 方法;请注意,我还删除了第一个
setSceneRect()
调用,这是因为将其保留为默认值可确保视图的sceneRect
始终更新为场景的sceneRect
,除非明确指定,否则默认为项目的边界矩形:最后,你真的不应该太在意内存的使用。QPoints的内存占用非常小,如果您担心系统无法支持这一点,那么您就偏离了轨道:您只需要大约2万个千个点就可以获得一兆字节的内存
注意与问题无关:我会避免不必要的过度使用类型暗示。Python始终是一种动态类型的语言,虽然使用它们并不被认为是不好的做法,但过多地到处使用它们只会分散注意力,特别是在处理库/框架内部使用的重写方法时;例如,
paint()
中的类型暗示是完全不必要的,因为您可以确保所有参数总是使用正确的类型调用相关问题 更多 >
编程相关推荐