7
votes

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);
    }
}
3

3 Answers

6
votes

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);
6
votes

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);
       }
    }
}
1
votes

I think your question has a bit simpler answer.. To disable scroll bars just set scroll bar policy (QGraphicsView is just QScrollView), so step 1)

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

that will disable scroll bars..

step 2) (if you want to keep it simple)

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

thats the fastest way to get results you want