旋转 - TransformOriginPoint - PyQt4 [英] Rotate - TransformOriginPoint - PyQt4
问题描述
我试图使用 PyQt 构建一个小型 UI.
I was trying to build a small UI using PyQt .
它在 QGraphicsView 中有一个窗口、一个按钮(旋转)和一个多边形(矩形).目的之一该应用程序允许用户旋转多边形.那是在单击按钮,用户单击一个点,即最近的顶点自动向用户点击移动或倾斜.我也设置了多边形在点击前可移动,点击后不可移动.
It has a window , a button(Rotate) , and a polygon(rectangle) in a QGraphicsView. One aim of the app is to allow the user to rotate the polygon. That is after the button is clicked and the user clicks at a point , the nearest vertex automatically shifts or tilts towards the user click. Ive also set the polygon to be movable before the click and not movable after the click.
问题是如果用户移动多边形然后点击,多边形以一种奇怪的方式旋转.有人可以帮我找出错误吗?我的猜测是它可能与 setTransformOriginPoint 相关.
The problem is if the user moves the polygon and then clicks , the polygon rotates in a weird manner. Could someone help me figure out the error? My guess is that it might be with the setTransformOriginPoint.
我有两个从 QtGui.QWidget 和 QtGui.QGraphicsScene 继承的类.
I have two classes inherited from QtGui.QWidget and QtGui.QGraphicsScene.
class Window(QtGui.QWidget):
def polychange(self , sender): //Called by the button ,
if sender:
self.view.polyrotate = 1 //self.view is an instance of QGraphicsScene class
self.view.polyf.setFlag(QtGui.QGraphicsItem.ItemIsMovable , False)
else:
self.view.polyrotate = 0
self.view.degrees = 0
self.view.polyf.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
class Example(QtGui.QGraphicsView):
def mousePressEvent(self , e):
super(Example , self).mousePressEvent(e)
self.x = e.x()
self.y = e.y()
if self.polyrotate == 1:
self.Rotate()
def Rotate(self):
self.pverticesx = []
self.pverticesy = []
distances = []
for i in range(4):
self.pverticesx.append(self.polyf.mapToScene(self.polyf.polygon()[i]).x())
self.pverticesy.append(self.polyf.mapToScene(self.polyf.polygon()[i]).y())
x1 = self.x
y1 = self.y
for i in range(4):
distance = math.hypot(self.pverticesx[i] - x1 , self.pverticesy[i] - y1)
distances.append(distance)
midpointx = (self.pverticesx[0] + self.pverticesx[2]) / 2
midpointy = (self.pverticesy[0] + self.pverticesy[2]) / 2
index = distances.index(min(distances))
pointx = self.pverticesx[index]
pointy = self.pverticesy[index]
vector1 = [x1 - midpointx , y1 - midpointy]
vector2 = [pointx - midpointx , pointy - midpointy]
num = 0
for i in [0 , 1]:
num = num + (vector1[i] * vector2[i])
den = math.sqrt(sum(map(lambda x : x * x , vector1))) * math.sqrt(sum(map(lambda x : x * x , vector2)))
degree = math.degrees(math.acos(num / den))
self.degrees = degree + self.degrees
if self.degrees > 360:
rotation = self.degrees / 360
self.degrees = self.degrees - (rotation * 360)
self.polyf.setTransformOriginPoint(midpointx , midpointy)
self.polyf.setRotation(self.degrees)
这是我的代码的更详尽的链接.旋转应用.提前致谢.
Here is a more exhaustive link to my code. RotateApp . Thanks in advance.
推荐答案
我已经找到了自己问题的答案.在旋转之前,midpointx 和 midpointy 在场景坐标中.所以我不得不将它们映射回 QGraphicsItem 坐标系统.self.setRotation() 之前的这一行可以解决问题.
I've found out the answer to my own question. Before rotating , midpointx and midpointy are in scene coordinates. So I had to map them back to QGraphicsItem coordinates system. This line before self.setRotation() would do the trick.
self.polyf.setTransformOriginPoint(self.polyf.mapFromScene(QtCore.QPointF(midpointx , midpointy)))
现在我的多边形不会随机移动.
Now my polygon doesnt move randomnly.
这篇关于旋转 - TransformOriginPoint - PyQt4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!