gpt4 book ai didi

Python Qt : Interactive Re-Sizable QGraphicsItem, 鼠标悬停区域不调整大小

转载 作者:太空狗 更新时间:2023-10-30 00:07:15 26 4
gpt4 key购买 nike

我正在尝试围绕 QGraphicsRectItem(PySide 或 PyQt4)构建一个 python 类,它通过悬停提供鼠标交互、可移动和可调整大小。我几乎所有的东西都在工作,除了:

出于某种原因,当项目重新调整大小或移动时,鼠标悬停区域似乎没有改变。我需要帮助解决这个问题。

也许问题是由于在 QGraphicsView 上反转 y 轴引起的:

QGraphicsView.scale(1,-1)

QGraphicsRectItem类:

class BoxResizable(QtGui.QGraphicsRectItem):
def __init__(self, rect, parent = None, scene = None):
QtGui.QGraphicsRectItem.__init__(self, rect, parent, scene)

self.setZValue(1000)
self._rect = rect
self._scene = scene
self.mouseOver = False
self.resizeHandleSize = 4.0

self.mousePressPos = None
self.mouseMovePos = None
self.mouseIsPressed = False

self.setFlags(QtGui.QGraphicsItem.ItemIsSelectable|QtGui.QGraphicsItem.ItemIsFocusable)
self.setAcceptsHoverEvents(True)

self.updateResizeHandles()

def hoverEnterEvent(self, event):
self.updateResizeHandles()
self.mouseOver = True
self.prepareGeometryChange()

def hoverLeaveEvent(self, event):
self.mouseOver = False
self.prepareGeometryChange()

def hoverMoveEvent(self, event):

if self.topLeft.contains(event.scenePos()) or self.bottomRight.contains(event.scenePos()):
self.setCursor(QtCore.Qt.SizeFDiagCursor)
elif self.topRight.contains(event.scenePos()) or self.bottomLeft.contains(event.scenePos()):
self.setCursor(QtCore.Qt.SizeBDiagCursor)
else:
self.setCursor(QtCore.Qt.SizeAllCursor)

QtGui.QGraphicsRectItem.hoverMoveEvent(self, event)

def mousePressEvent(self, event):
"""
Capture mouse press events and find where the mosue was pressed on the object
"""
self.mousePressPos = event.scenePos()
self.mouseIsPressed = True
self.rectPress = copy.deepcopy(self._rect)

# Top left corner
if self.topLeft.contains(event.scenePos()):
self.mousePressArea = 'topleft'
# top right corner
elif self.topRight.contains(event.scenePos()):
self.mousePressArea = 'topright'
# bottom left corner
elif self.bottomLeft.contains(event.scenePos()):
self.mousePressArea = 'bottomleft'
# bottom right corner
elif self.bottomRight.contains(event.scenePos()):
self.mousePressArea = 'bottomright'
# entire rectangle
else:
self.mousePressArea = None

QtGui.QGraphicsRectItem.mousePressEvent(self, event)

def mouseReleaseEvent(self, event):
"""
Capture nmouse press events.
"""
self.mouseIsPressed = False

self.updateResizeHandles()
self.prepareGeometryChange()

QtGui.QGraphicsRectItem.mouseReleaseEvent(self, event)

def mouseMoveEvent(self, event):
"""
Handle mouse move events.
"""
self.mouseMovePos = event.scenePos()

if self.mouseIsPressed:
# Move top left corner
if self.mousePressArea=='topleft':
self._rect.setTopLeft(self.rectPress.topLeft()-(self.mousePressPos-self.mouseMovePos))
# Move top right corner
elif self.mousePressArea=='topright':
self._rect.setTopRight(self.rectPress.topRight()-(self.mousePressPos-self.mouseMovePos))
# Move bottom left corner
elif self.mousePressArea=='bottomleft':
self._rect.setBottomLeft(self.rectPress.bottomLeft()-(self.mousePressPos-self.mouseMovePos))
# Move bottom right corner
elif self.mousePressArea=='bottomright':
self._rect.setBottomRight(self.rectPress.bottomRight()-(self.mousePressPos-self.mouseMovePos))
# Move entire rectangle, don't resize
else:
self._rect.moveCenter(self.rectPress.center()-(self.mousePressPos-self.mouseMovePos))

self.updateResizeHandles()
self.prepareGeometryChange()

QtGui.QGraphicsRectItem.mousePressEvent(self, event)

def boundingRect(self):
"""
Return bounding rectangle
"""
return self._boundingRect

def updateResizeHandles(self):
"""
Update bounding rectangle and resize handles
"""
self.offset = self.resizeHandleSize*(self._scene.graphicsView.mapToScene(1,0)-self._scene.graphicsView.mapToScene(0,1)).x()

self._boundingRect = self._rect.adjusted(-self.offset, self.offset, self.offset, -self.offset)

# Note: this draws correctly on a view with an inverted y axes. i.e. QGraphicsView.scale(1,-1)
self.topLeft = QtCore.QRectF(self._boundingRect.topLeft().x(), self._boundingRect.topLeft().y() - 2*self.offset,
2*self.offset, 2*self.offset)
self.topRight = QtCore.QRectF(self._boundingRect.topRight().x() - 2*self.offset, self._boundingRect.topRight().y() - 2*self.offset,
2*self.offset, 2*self.offset)
self.bottomLeft = QtCore.QRectF(self._boundingRect.bottomLeft().x(), self._boundingRect.bottomLeft().y(),
2*self.offset, 2*self.offset)
self.bottomRight = QtCore.QRectF(self._boundingRect.bottomRight().x() - 2*self.offset, self._boundingRect.bottomRight().y(),
2*self.offset, 2*self.offset)

def paint(self, painter, option, widget):
"""
Paint Widget
"""

# show boundingRect for debug purposes
painter.setPen(QtGui.QPen(QtCore.Qt.red, 0, QtCore.Qt.DashLine))
painter.drawRect(self._boundingRect)

# Paint rectangle
painter.setPen(QtGui.QPen(QtCore.Qt.black, 0, QtCore.Qt.SolidLine))
painter.drawRect(self._rect)

# If mouse is over, draw handles
if self.mouseOver:
# if rect selected, fill in handles
if self.isSelected():
painter.setBrush(QtGui.QBrush(QtGui.QColor(0,0,0)))
painter.drawRect(self.topLeft)
painter.drawRect(self.topRight)
painter.drawRect(self.bottomLeft)
painter.drawRect(self.bottomRight)

功能示例的其余代码:

class graphicsScene(QtGui.QGraphicsScene):
def __init__ (self, parent = None):
QtGui.QGraphicsScene.__init__(self, parent)
def setGraphicsView(self, view):
self.graphicsView = view

app = QtGui.QApplication(sys.argv)
app.setStyle('GTK')
mainWindow = QtGui.QMainWindow()

scene = graphicsScene()
scene.setSceneRect(-100,-100, 200, 200)

# Set up view properties
view = QtGui.QGraphicsView()
view.setScene(scene)
view.scale(1,-1)
view.setRenderHint(QtGui.QPainter.Antialiasing)
view.setViewportUpdateMode(QtGui.QGraphicsView.BoundingRectViewportUpdate)

view.setHorizontalScrollBarPolicy ( QtCore.Qt.ScrollBarAlwaysOff )
view.setVerticalScrollBarPolicy ( QtCore.Qt.ScrollBarAlwaysOff )
view.setUpdatesEnabled(True)
view.setMouseTracking(True)

view.setCacheMode(QtGui.QGraphicsView.CacheBackground)

view.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)

scene.setGraphicsView(view)

# Add box
BoxResizable(QtCore.QRectF(-50, 50, 100.0, -100.0), parent = None, scene = scene)

mainWindow.setCentralWidget(view)
mainWindow.show()

app.exec_()

app.deleteLater()
sys.exit()

将上述类添加到 QGraphicsScene 和随后的反转 y 轴 QGraphicsView 将产生如下内容:

enter image description here

如有任何帮助或建议,我们将不胜感激!谢谢!

最佳答案

发现部分问题:如果我覆盖形状函数(将此函数添加到类中):

    def shape(self):
path = QtGui.QPainterPath()
path.addRect(self.boundingRect())
return path

悬停区域有时会随着框的大小和位置而变化。根据docs对于 QGraphicsItem.shape():

The default implementation calls boundingRect() to return a simple rectangular shape...

这是 PyQt4 中的错误吗?

第二个问题:我认为boundingRect 和shape rectangle 需要有正的宽度和高度?这很容易通过添加来完成:

self._rect = self._rect.normalized()

mouseReleaseEvent 函数。

关于Python Qt : Interactive Re-Sizable QGraphicsItem, 鼠标悬停区域不调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22226587/

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