gpt4 book ai didi

python - sceneRect().translated(x,y) 无法与缩放正常工作

转载 作者:行者123 更新时间:2023-12-01 00:09:03 25 4
gpt4 key购买 nike

一个月前,我问,如何翻译场景,我得到了很大的帮助,并理解了它如何工作,在使用它/添加人员一个月后,我注意到当你放大到非常接近几何体或某处时否则,翻译就会变得越来越有值(value)。

您必须放大到非常接近几何体才能看到问题。这是一些代码:

from PyQt5.QtGui import QColor, QPolygonF, QPen, QBrush
from PyQt5.QtCore import Qt, QPointF, QPoint, pyqtSignal
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsPolygonItem, QApplication, \
QFrame, QSizePolicy
points_list = [[60.1, 19.6, 0.0], [60.1, 6.5, 0.0], [60.1, -6.5, 0.0], [60.1, -19.6, 0.0], [60.1, -19.6, 0.0],
[20.0, -19.6, 0.0], [-20, -19.6, 0.0], [-60.1, -19.6, 0.0], [-60.1, -19.6, 0.0], [-60.1, -6.5, 0.0],
[-60.1, 6.5, 0.0], [-60.1, 19.6, 0.0], [-60.1, 19.6, 0.0], [-20.0, 19.6, 0.0], [20.0, 19.6, 0.0],
[60.1, 19.6, 0.0]]


class MainWindow(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent=parent)
self.create()

def create(self, **kwargs):
main_layout = QVBoxLayout()
graphics = MainGraphicsWidget()
main_layout.addWidget(graphics)
self.setLayout(main_layout)

class MainGraphicsWidget(QGraphicsView):
zoom_signal = pyqtSignal(bool)

def __init__(self, parent=None):
super(MainGraphicsWidget, self).__init__(parent)
self._scene = QGraphicsScene(backgroundBrush=Qt.gray)
self.__zoom = 0
self.setScene(self._scene)
self.setTransformationAnchor(QGraphicsView.NoAnchor)
#self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
self.setFrameShape(QFrame.NoFrame)
self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
# self.sceneRect = self._scene.sceneRect()
self.testButton = GraphicsButton()
self._scene.addItem(self.testButton)
self.startPos = None


def mousePressEvent(self, event):
if event.modifiers() & Qt.AltModifier and event.button() == Qt.LeftButton:
self.startPos = event.pos()
else:
super(MainGraphicsWidget, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
modifierPressed = QApplication.keyboardModifiers()
if self.startPos is not None:
delta = self.startPos - event.pos()
transform = self.transform()
deltaX = delta.x() / transform.m11()
deltaY = delta.y() / transform.m22()
self.setSceneRect(self.sceneRect().translated(deltaX, deltaY))
self.startPos = event.pos()
else:
super(MainGraphicsWidget, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
self.startPos = None
super(MainGraphicsWidget, self).mouseReleaseEvent(event)


def wheelEvent(self, event):
if event.angleDelta().y() > 0:
factor = 1.25
self.__zoom += 1
else:
factor = 0.8
self.__zoom -= 1
self.scale(factor, factor)
self.zoom_signal.emit(self.__zoom < 10)


class GraphicsButton(QGraphicsPolygonItem):
def __init__(self, parent=None):
super(GraphicsButton, self).__init__(parent)
self.myPolygon = QPolygonF([QPointF(v1, v2) for v1, v2, v3 in points_list])
self.setPen(QPen(QColor(0, 0, 0), 0, Qt.SolidLine, Qt.FlatCap, Qt.MiterJoin))
self.setPolygon(self.myPolygon)
self.setBrush(QColor(220, 40, 30))


if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.setGeometry(500, 100, 500, 900)
window.show()
sys.exit(app.exec_())

(翻译开始使用Alt + 左键单击)

我猜问题是,当你放大得太近时,你的光标仍然沿着相同的坐标移动(它们不会变小)并且它开始翻译得更慢,我在代码中没有考虑这一点。但为什么有时我的 sceneRect().translated() 根本停止移动呢?我是否放大到至少不与光标交叉的点(1x,1y)?

所以我的问题是,如果我的理论正确,有人可以建议我如何计算delta、放大和缩小吗?谢谢你,抱歉我的英语不好

编辑:这是您需要放大多少才能看到问题的屏幕截图。

enter image description here

最佳答案

sceneRect位于场景的坐标中,而不是 View 的坐标中,因此为了进行计算,您必须使用 mapToScene 方法将鼠标点转换为场景的坐标。此修改还应该应用于投影在场景中的视口(viewport)矩形,因为它不等于 sceneRect

class MainGraphicsWidget(QGraphicsView):
zoom_signal = pyqtSignal(bool)

def __init__(self, parent=None):
# ...
self.startPos = QPointF()

def mousePressEvent(self, event):
if event.modifiers() & Qt.AltModifier and event.button() == Qt.LeftButton:
self.startPos = self.mapToScene(event.pos())
else:
super(MainGraphicsWidget, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
if not self.startPos.isNull():
delta = self.startPos - self.mapToScene(event.pos())
r = self.mapToScene(self.viewport().rect()).boundingRect()
self.setSceneRect(r.translated(delta))
else:
super(MainGraphicsWidget, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
self.startPos = QPointF()
super(MainGraphicsWidget, self).mouseReleaseEvent(event)

def wheelEvent(self, event):
# ...

关于python - sceneRect().translated(x,y) 无法与缩放正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59768115/

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