我有一个构成符号的路径,我想知道QPainter
是否可以绘制该路径。阅读文档时,我看到有一个与drawPath
相关联的QPainter
函数,但是每次尝试实现该函数时,都会不断收到一个错误,提示QPainter::drawPath: Painter not active
。
因为我没有任何成功的尝试,所以我添加了代码的最小化工作版本。它将为您提供一些基本的缩放和平移功能以及选择符号,并按住Shift键以取消选择符号。
我希望在选中或将鼠标悬停在路径上时更改颜色,并且当前在选中或将鼠标悬停在路径上时将其填充。最终,我希望用户能够从一开始就将路径更改为虚线或颜色。
谢谢您的建议和帮助。
代码
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSvg import *
import sys
from math import sqrt,cos,acos,asin,degrees,pi
class LogObject(QObject):
hovered = pyqtSignal()
notHovered = pyqtSignal()
def figAngle(radius,opposite):
dist = sqrt((radius) ** 2 + (opposite) ** 2)
angle = degrees(asin(opposite/dist))
if angle < 0:
angle = angle + 360
return angle
def create_path():
scale = 250
path = QPainterPath()
path.addEllipse(QPointF(0,0), 0.0268, 0.0268) # Using QPointF will center it
startAngle = figAngle(0.0357/scale,0.0045/scale) # Use the scale so that it will adjust as it is scaled
endAngle = figAngle(0.0357/scale,-0.0092/scale)
path.moveTo((0.0357+0.0821),0.0045 )
path.arcTo(QRectF(-0.0357,-0.0357,(0.0357*2),(0.0357*2)),-startAngle,-endAngle)
path.moveTo(0.0357, -0.0045)
path.lineTo(0.0357 + 0.0821, -0.0045)
path.lineTo(0.0357+0.0821,-0.0045-0.0680)
path.lineTo(0.0357+0.0821+0.1450,-0.0045-0.0680)
path.lineTo(0.0357+0.0821+0.1450,-0.0045-0.0680+0.1450)
path.lineTo(0.0357+0.0821,-0.0045-0.0680+0.1450)
path.lineTo(0.0357+0.0821,-0.0045-0.0680+0.1450-0.0680)
path.moveTo(0.0357+0.0821+0.0090,-0.0045-0.0680+0.1450-0.0090)
path.addRect(0.0357+0.0821+0.0090, -0.0045-0.0680+0.1450-0.0090-0.1270, 0.1270, 0.1270)
tr = QTransform()
tr.scale(scale, scale)
path = tr.map(path)
return path
class Point(QGraphicsPathItem):
def __init__(self, x, y, r, name):
super(Point, self).__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setPath(create_path())
self.setScale(1)
self.setRotation(180+r)
self.setAcceptHoverEvents(True)
self.log = LogObject()
self.setPos(x, y)
self.isSelected = False
def itemChange(self, change, value):
if change == self.ItemSelectedChange:
self.setBrush(QBrush(Qt.green) if value else QBrush(Qt.black))
return QGraphicsItem.itemChange(self, change, value)
def hoverEnterEvent(self, event):
self.setBrush(QColor("red"))
self.log.hovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)
def hoverLeaveEvent(self, event):
if self.isSelected == True:
self.setBrush(QBrush(Qt.green))
else:
self.setBrush(QColor("black"))
self.log.notHovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)
class Viewer(QGraphicsView):
photoClicked = pyqtSignal(QPoint)
rectChanged = pyqtSignal(QRect)
def __init__(self, parent):
super(Viewer, self).__init__(parent)
self.setRenderHints(QPainter.Antialiasing)
self._zoom = 0
self._empty = True
self.setScene(QGraphicsScene(self))
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setFrameShape(QFrame.NoFrame)
self.area = float()
self.setPoints()
self.viewport().setCursor(Qt.ArrowCursor)
QTimer.singleShot(0, self.reset_fit)
self.selectedItems = []
def setItems(self):
self.data = {
"x": [
-2414943.8686,
-2417160.6592,
-2417160.6592,
-2416009.9966,
-2416012.5232,
-2416012.5232,
],
"y": [
10454269.7008,
10454147.2672,
10454147.2672,
10453240.2808,
10455255.8752,
10455255.8752,
],
"rotation":[
313.9962,
43.9962,
223.9962,
313.9962,
43.9962,
223.9962,
]
}
for i, (x, y,r) in enumerate(zip(self.data["x"], self.data["y"],self.data["rotation"])):
p = Point(x, y,r, "Point__" + str(i))
p.log.hovered.connect(self.hoverChange)
p.log.notHovered.connect(self.notHoverChange)
self.scene().addItem(p)
def setPoints(self):
self.setItems()
self.setDragMode(self.ScrollHandDrag)
def wheelEvent(self, event):
if event.angleDelta().y() > 0:
factor = 1.25
self._zoom += 1
else:
factor = 0.8
self._zoom -= 1
if self._zoom > 0:
self.scale(factor, factor)
elif self._zoom == 0:
self.reset_fit()
else:
self._zoom = 0
def hoverChange(self):
self.viewport().setCursor(Qt.PointingHandCursor)
def notHoverChange(self):
self.viewport().setCursor(Qt.ArrowCursor)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
singleItem = self.itemAt(event.pos().x(), event.pos().y())
if singleItem != None:
if QApplication.keyboardModifiers() == Qt.ShiftModifier: # This will determine if the shift key is depressed
if singleItem.isSelected == True:
singleItem.setSelected(False)
singleItem.isSelected = False
self.selectedItems.remove(singleItem)
elif singleItem.isSelected == False:
singleItem.setSelected(True)
singleItem.isSelected = True
self.selectedItems.append(singleItem)
return
elif event.button() == Qt.MidButton:
self.viewport().setCursor(Qt.ClosedHandCursor)
self.original_event = event
handmade_event = QMouseEvent(
QEvent.MouseButtonPress,
QPointF(event.pos()),
Qt.LeftButton,
event.buttons(),
Qt.KeyboardModifiers(),
)
QGraphicsView.mousePressEvent(self, handmade_event)
super(Viewer, self).mousePressEvent(event)
def mouseReleaseEvent(self, event):
if event.button() == Qt.MidButton:
self.viewport().setCursor(Qt.ArrowCursor)
handmade_event = QMouseEvent(
QEvent.MouseButtonRelease,
QPointF(event.pos()),
Qt.LeftButton,
event.buttons(),
Qt.KeyboardModifiers(),
)
QGraphicsView.mouseReleaseEvent(self, handmade_event)
def reset_fit(self):
r = self.scene().itemsBoundingRect()
self.resetTransform()
self.setSceneRect(r)
self.fitInView(r, Qt.KeepAspectRatio)
self._zoom = 0
self.scale(1, -1)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.viewer = Viewer(self)
VBlayout = QVBoxLayout(self)
VBlayout.addWidget(self.viewer)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 800, 600)
window.show()
sys.exit(app.exec_())
答案 0 :(得分:2)
您不必在QGraphicsPathItem中使用QPainter,而必须使用setPen()设置QPen。
QGraphicsPathItem已经有一个QPainter,仅应在paint()方法中使用,但是在这种情况下,没有必要使用现有的QGraphicsItems(例如QGraphicsRectItem,QGraphicsEllipseItem等),因为这些方法继承自QAbstractGraphicsShapeItem并分别使用setPen()和setBrush()方法支持QPen和QBrush。
我在您的代码中看到的另一个问题是您正在使用isSelected属性,但您不应这样做,因为已经有一个具有该名称的方法,所以您必须使用isSelected()。
考虑先前的解决方案是:
class Point(QGraphicsPathItem):
def __init__(self, x, y, r, name):
super(Point, self).__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setPath(create_path())
self.setScale(10)
self.setRotation(180 + r)
self.setAcceptHoverEvents(True)
self.log = LogObject()
self.setPos(x, y)
pen = QPen(Qt.red)
pen.setStyle(Qt.DashLine)
self.setPen(pen)
def itemChange(self, change, value):
if change == self.ItemSelectedChange:
color = QColor(Qt.green) if value else QColor("black")
# self.setBrush(color)
pen = self.pen()
pen.setColor(color)
self.setPen(pen)
return QGraphicsItem.itemChange(self, change, value)
def hoverEnterEvent(self, event):
color = QColor("red")
# self.setBrush(color)
pen = self.pen()
pen.setColor(color)
self.setPen(pen)
self.log.hovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)
def hoverLeaveEvent(self, event):
color = QColor(Qt.green) if self.isSelected() else QColor("black")
# self.setBrush(color)
pen = self.pen()
pen.setColor(color)
self.setPen(pen)
self.log.notHovered.emit()
QGraphicsItem.hoverMoveEvent(self, event)