Remove QGraphics Wheel Scrolling Functionality

I have a QGraphicsView window on my widget and just added an event for the mouse wheel that enlarges the image.

However, as soon as I click on the scroll bars and the scroll functions on the mouse wheel override the zoom function that I have.

I was wondering if there is a way to remove the scroll all together and add a drag and drop option to move, or maybe CTRL and the mouse wheel to scale, and the mouse wheel will control the scroll

here is my zoom function (which they know is not perfect), but if someone can shed light on the fact that it will be a bonus

welcomes in advance

void Test::wheelEvent(QWheelEvent *event)
{
    if(event->delta() > 0)
    {
        ui->graphicsView->scale(2,2);
    }
    else
    {
        ui->graphicsView->scale(0.5,0.5);
    }
}
+5
source share
3 answers

wheelEvent QWidget/QMainWindow, QGraphicsView, wheelEvent QGraphicsView .

QGraphicsView, wheelEvent QGraphicsView - wheelEvent QWidget/QMainWindow, reimplemented wheelEvent, , . - :

:

class myQGraphicsView : public QGraphicsView
{
public:
    myQGraphicsView(QWidget * parent = nullptr);
    myQGraphicsView(QGraphicsScene * scene, QWidget * parent = nullptr);

protected:
    virtual void wheelEvent(QWheelEvent * event);
};

:

myQGraphicsView::myQGraphicsView(QWidget * parent)
: QGraphicsView(parent) {}

myQGraphicsView::myQGraphicsView(QGraphicsScene * scene, QWidget * parent)
: QGraphicsView(scene, parent) {}

void myQGraphicsView::wheelEvent(QWheelEvent * event)
{
    // your functionality, for example:
    // if ctrl pressed, use original functionality
    if (event->modifiers() & Qt::ControlModifier)
    {
        QGraphicsView::wheelEvent(event);
    }
    // otherwise, do yours
    else
    {
       if (event->delta() > 0)
       {
           scale(2, 2);
       }
       else
       {
           scale(0.5, 0.5);
       }
    }
}
+4

:

    ui->graphicsView->verticalScrollBar()->blockSignals(true);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->horizontalScrollBar()->blockSignals(true);
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+3

, . , (QGraphicsView - QScrollView), 1)

setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

.

2) ( )

QGraphicsView * pView;  // pointer to your graphics view
pView->setInteractive(true);
pView->setDragMode(QGraphicsView::ScrollHandDrag);

.

+1

All Articles