Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove scroll functionality on mouse wheel QGraphics view

Tags:

c++

qt

I have a QGraphicsView window on my widget and have just put in an event for mouse wheel which zooms in on the image.

However as soon as i zoom in scroll bars are displayed and the scroll functionality on the mouse wheel overrides the zoom function i have.

i was wondering if there is any way that i can remove scrolling all together and add a drag to move option or maybe a CTRL and mouse wheel to zoom and mouse wheel alone would control scrolling

here is my zoom function (Which im aware isnt perfect) but if anyone could shed some light on that it would be a bonus

cheers in advance

void Test::wheelEvent(QWheelEvent *event)
{
    if(event->delta() > 0)
    {
        ui->graphicsView->scale(2,2);
    }
    else
    {
        ui->graphicsView->scale(0.5,0.5);
    }
}
like image 985
AngryDuck Avatar asked Apr 19 '13 13:04

AngryDuck


2 Answers

You reimplemented wheelEvent for QWidget/QMainWindow that contains your QGraphicsView, however, wheelEvent of QGraphicsView remains intact.

You can derive from QGraphicsView, reimplement wheelEvent for derived class and use derive class instead of QGraphicsView - this way you won't even need wheelEvent in your QWidget/QMainWindow, and you can customize reimplemented wheelEvent to do what you want. Something like that:

Header file:

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

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

Source file:

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);
       }
    }
}
like image 198
Ilya Kobelevskiy Avatar answered Oct 06 '22 21:10

Ilya Kobelevskiy


Scrolling can be disabled with the following code:

    ui->graphicsView->verticalScrollBar()->blockSignals(true);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->horizontalScrollBar()->blockSignals(true);
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
like image 20
psypod Avatar answered Oct 06 '22 22:10

psypod