视图QGraphicsView
QGraphicsView提供视图部件,可视化场景中的内容。坐标系是左上角为原点(0,0)。
场景QGraphicsScene
可以看成是QGraphicsItem的容器。注意设置QGraphicsScene的大小。否则在进行坐标转换的时候回出现问题。
QGraphicsItem
item 的坐标是在自己的坐标系中,item的中心点为(0,0)。
简单的使用详情代码http://download.youkuaiyun.com/detail/u011962883/9890966
缩放实现
void GraphicsItem::resizeto(ZoomPointRect::Direction dir,QPointF point) { QPointF local = mapFromScene(point); QRect delta = this->rect().toRect(); switch(dir) { case ZoomPointRect::LeftTop: delta.setTopLeft(local.toPoint()); break; case ZoomPointRect::Top: delta.setTop(local.y()); break; case ZoomPointRect::RightTop: delta.setTopRight(local.toPoint()); break; case ZoomPointRect::Right: delta.setRight(local.x()); break; case ZoomPointRect::RightBottom: delta.setBottomRight(local.toPoint()); break; case ZoomPointRect::Bottom: delta.setBottom(local.y()); break; case ZoomPointRect::LeftBottom: delta.setBottomLeft(local.toPoint()); break; case ZoomPointRect::Left: delta.setLeft(local.x()); break; } prepareGeometryChange(); m_width = delta.width(); m_height = delta.height(); updateZoomPoint(); } |
在场景scence中获取到坐标的坐标在item中使用mapFromScene进行坐标转换,至于为什么,打印一下坐标就知道了,你就会发现同一个位置的坐标是不同的。根据鼠标缩放的方向进行缩放。
关于旋转问题
qreal GraphicsScene::rotationAngle(GraphicsItem *item, QPointF point) { QPointF local = item->mapFromScene(point); item->setTransformOriginPoint(item->boundingRect().center()); qreal angle = atan2(local.y(),local.x())*180/PI; return angle; } |
setTransformOriginPoint设置item的旋转点,使用atan2函数计算出角度,item->setRotation(angle);设置角度。
在代码中遇到的问题
1.从scence获取到的坐标转换到item时,坐标转换出问题
原因是因为没有设置scence->setSceneRect(640/2,350/2,640,450);