Qt绘图QGraphicsView、QGraphicsScene、QGraphicsItem

视图QGraphicsView

QGraphicsView提供视图部件,可视化场景中的内容。坐标系是左上角为原点(0,0)。

场景QGraphicsScene

可以看成是QGraphicsItem的容器。注意设置QGraphicsScene的大小。否则在进行坐标转换的时候回出现问题。

QGraphicsItem

item 的坐标是在自己的坐标系中,item的中心点为(0,0)。

简单的使用详情代码http://download.csdn.net/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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值