signed

QiShunwang

“诚信为本、客户至上”

QT中用鼠标移动QGraphicsView

2021/4/26 17:46:08   来源:

@1

QT中通过操纵滚动条实现鼠标移动QGraphicsView的展示部分

第一次写

代码

void OCRGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
   Q_UNUSED(event)

   if (sign && m_IsMoveAble )
   {
       QPointF newPos = event->pos();
       QPointF MoveStep = newPos - lastPos;//计算移动坐标
       //获取View的长宽
       qreal viewWidth = this->viewport()->width();
       qreal viewHeight = this->viewport()->height();
       // 获取当前移动坐标相当于view大小的横纵比例;
       qreal hScale = MoveStep.x() / viewWidth;
       qreal vScale = MoveStep.y() / viewHeight;
       //通过滚动条调整展示的scene位置
        this->horizontalScrollBar()->
        setValue(this->horizontalScrollBar()->value() - viewWidth*hScale);
        this->verticalScrollBar()->
        setValue(this->verticalScrollBar()->value() - viewHeight*vScale);
    }
    lastPos = event->pos();
    QGraphicsView::mouseMoveEvent(event);
}