You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QGraphicsView滚轮缩放异常:Y轴位置随机跳动问题求助

Qt GraphicsView缩放时Y轴位置随机跳动问题

我在Qt中实现缩放功能,尝试过多种常见方法,但都无效。缩放比例正常,但滚轮操作后Y轴位置会随机跳动(X轴位置看似正常)。

复现步骤:

  • 创建Qt Widgets Application项目
  • 在设计页面,从“Display Widgets”中拖拽一个Graphics View(默认objectName为graphicsView)
  • 在MainWindow.h中声明1个变量、3个函数和1个私有槽函数:
public:
    QGraphicsView* m_pDrawingArea;
    void setupView();
    void setupDefaultDrawing();
    void eventDrawingArea_Wheel(QWheelEvent* event);
private slots:
    bool eventFilter(QObject* object, QEvent* event) override;
  • 在构造函数中初始化变量并调用函数:
ui->setupUi(this);
m_pDrawingArea = findChild<QGraphicsView*>("graphicsView");
setupView();
setupDefaultDrawing();
  • 实现setupView()和setupDefaultDrawing()函数:
void MainWindow::setupView(){
    QGraphicsScene* pScene = new QGraphicsScene(this);
    m_pDrawingArea->setScene(pScene);
    //反转Y轴
    m_pDrawingArea->scale(1, -1);
    //开启鼠标追踪
    m_pDrawingArea->setMouseTracking(true);
    m_pDrawingArea->viewport()->installEventFilter(this);
    //设置视口更新模式为全视口更新
    m_pDrawingArea->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}
void MainWindow::setupDefaultDrawing(){
    //绘制坐标轴扩展场景范围
    int width = m_pDrawingArea->width()*100;
    int height = m_pDrawingArea->height()*100;
    QGraphicsLineItem* pXAxis = new QGraphicsLineItem(-width / 2, 0, width / 2, 0);
    QGraphicsLineItem* pYAxis = new QGraphicsLineItem(0, -height / 2, 0, height / 2);
    m_pDrawingArea->scene()->addItem(pXAxis);
    m_pDrawingArea->scene()->addItem(pYAxis);
    //添加测试用点
    QGraphicsEllipseItem* pPoint = new QGraphicsEllipseItem(100, 100, 1, 1);
    pPoint->setPen(QPen(Qt::black, 1, Qt::SolidLine));
    pPoint->setBrush(QBrush(Qt::black));
    m_pDrawingArea->scene()->addItem(pPoint);
}
  • 实现鼠标滚轮事件处理:
bool MainWindow::eventFilter(QObject* object, QEvent* event){
    if (object == m_pDrawingArea->viewport() && event->type() == QEvent::Wheel){
        eventDrawingArea_Wheel(static_cast<QWheelEvent*>(event));
    }
    return QMainWindow::eventFilter(object, event);
}
void MainWindow::eventDrawingArea_Wheel(QWheelEvent* wheelEvent){
    QPointF oldPos = m_pDrawingArea->mapToScene(wheelEvent->pos());
    if (wheelEvent->modifiers() & Qt::ControlModifier) {
        //处理缩放逻辑
        QGraphicsView::ViewportAnchor anchor = m_pDrawingArea->transformationAnchor();
        m_pDrawingArea->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
        int angle = wheelEvent->angleDelta().y();
        qreal scaleFactor = (angle > 0) ? 1.25 : 1 / 1.25;
        m_pDrawingArea->scale(scaleFactor, scaleFactor);
        QPointF newPos = m_pDrawingArea->mapToScene(wheelEvent->pos());
        m_pDrawingArea->translate(newPos.x() - oldPos.x(), newPos.y() - oldPos.y());
        m_pDrawingArea->setTransformationAnchor(anchor);
    } else {
//            QGraphicsView::wheelEvent(wheelEvent);
    }
}

内容的提问来源于stack exchange,提问作者BoKuan Liu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 23:15:00