gpt4 book ai didi

python - PyQt5 QGraphicsPathItem 由 QPainter 绘制

转载 作者:行者123 更新时间:2023-12-01 01:01:33 32 4
gpt4 key购买 nike

我有一条构成符号的路径,我想知道 QPainter 是否可以绘制该路径。在阅读文档时,我看到有一个与 QPainter 关联的 drawPath 函数,但每次我尝试实现它时,我总是收到一个错误,提示 QPainter::drawPath:画家未激活

因为我没有任何成功的尝试,所以我添加了代码的最小化工作版本。它将为您提供一些基本的缩放和平移功能以及选择符号以及按住 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_())

最佳答案

您不必在 QGraphicsPathItem 中使用 QPainter,您必须使用 setPen() 设置 QPen。

<小时/>

TL;博士;

QGraphicsPathItem已经有一个只能在paint()方法中使用的QPainter,但在这种情况下没有必要,现有的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)

关于python - PyQt5 QGraphicsPathItem 由 QPainter 绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754220/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com