QGraphicItem实现中心旋转

来源:互联网 发布:mac磁盘管理工具 编辑:程序博客网 时间:2024/05/16 15:17
QGraphicItem实现中心旋转
一、解决思路
1)确定旋转中心坐标:centerPos;
2)在mousePressEvent(QMouseEvent *e)中e->pos()获得按下时的坐标:pressPos
3)mouseMoveEvent(QMouseEvent *e)中e->pos()获得当前坐标:movePos;
4)centerPos、pressPos、movePos三点确定旋转角度:rotateAngle;
5)QTransform transform; 
transform.rotate(rotateAngle, Qt::ZAix); //沿着Z轴旋转角度rotateAngle
this->setTransform(transform);
6)Done。

二、 示意图


 
获取旋转中心:
QPointF centerPos=Item->boundingRect().center();
设置旋转中心:
void QGraphicsItem::setTransformOriginPoint(qreal x, qreal y)
设置旋转角度:
void QGraphicsItem::setRotation(qreal angle)
三、难点
1.角度确定

采用余弦定理:


 
2.判断是顺时针还是逆时针
思路:在平面上作向量OA、OB,OA叉乘OB为正,则是逆时针,为负为顺时针.
过程:
记向量OA为(dx1, dy1) = (x2-x1, y2-y1)向量OB为(dx2, dy2) = (x3-x1, y3-y1)叉积为:
即:
dx1 * dy2 - dy1 * dx2 
3.鼠标根据在Item的位置进行形状的改变
思路:通过判断鼠标在Item的位置进行形状的替换,可设置四种形状:
RotationLeftUp,RotationLeftDown,RotationRightUp,RotationRightDown.旋转的鼠标样式可以自定义。
QCursor *myCursor= newe QCursor(QPixmap(":/image/rotation.png"));

setCursor(*myCursor);



http://blog.csdn.net/u013207966/article/details/51892289

原创粉丝点击