繁体   English   中英

如何在 QGraphicsView 中禁用滚动?

[英]How to disable scrolling in QGraphicsView?

我正在尝试实现鼠标滚轮放大/缩小。 它可以工作,但是当我放大/缩小时,图像变得越来越小,并且在缩放 function 的同时向上/向下滚动。 看起来事件同时存在并协同工作。

我找不到如何禁用鼠标滚轮滚动。 可能有一种方法可以仅使用鼠标 cursor(通过单击滚动条)进行滚动。

我覆盖了鼠标滚轮的主要方法,但它导致了我上面写的效果

void MainWindow::wheelEvent( QWheelEvent* event )

通过使用事件过滤器解决。 下面的代码提供了按住 ctrl 按钮的放大/缩小功能。

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::GraphicsSceneWheel)
    {
        ui->GV_image->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
        double scaleFactor = 1.15;
        bool ok = QApplication::keyboardModifiers() & Qt::ControlModifier;
        if (ok)
        {
            QGraphicsSceneWheelEvent *scrollevent = static_cast<QGraphicsSceneWheelEvent *>(event);
            if (scrollevent->delta() > 0)
            {
                ui->GV_image->scale(scaleFactor, scaleFactor);
            }
            else
            {
                ui->GV_image->scale(1/scaleFactor, 1/scaleFactor);
            }
        }

        event->accept();
        return true;
    }
    return false;
}

将此行放入您的构造函数或其他您的 init function

    QGraphicsView *GV_image;

...

    ui->GV_image->scene()->installEventFilter(this);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM