PDA

View Full Version : How to disable wheelEvent for scrollbars



forrestfsu
11th October 2006, 16:35
I currently have a QGraphicsView with a QGraphicsScene set on it. I'm currently using the wheelevent to zoom in/out. However, whenever I use the wheel it first tries to move the image up or down (respective to scrolling wheel up or down). I don't want it to move the image up or down, I only want to use the wheel for zooming. Is there a way in my wheelEvent(QWheelEvent *event) function to only state to run the zoom code, and nothing else?

Thanks in advance.

Antrax
11th October 2006, 17:15
Have you tried doing event->accept() at the end of your function?

forrestfsu
11th October 2006, 19:11
I tried giving that a shot. But I think the problem might be a bit deeper. I have the two following functions:

void form::wheelEvent(QWheelEvent *event)
bool form::eventFilter(QObject *o, QEvent *e)

I originally put all my event calls into the eventFilter function, but for some reason was not able to call the "e->delta()" function in the eventFilter function. So I had to put it inside the wheelEvent function in order to use "event->delta()". It seems that my eventFilter is being called prior to the wheelEvent function. But even if I put "return false" into eventFilter and "event->accept()" into wheelEvent, it doesn't seem to work. If this is unclear, feel free to ask questions and I will specify. Unfortunately I cannot paste my code because my development box is not on the internet.

jpn
11th October 2006, 19:24
You should return true in an eventFilter() to make the event filtered out from the receiver. So if you have installed an event filter for the graphics view, return true always when the graphics view is receiving a wheel event and the graphics view never receives it. To access QWheelEvent::delta(), you need to cast the QEvent* to a QWheelEvent*.

forrestfsu
11th October 2006, 19:51
Thanks guys for helping me try to solve this. I'm still having a problem though...I tried to follow your advice, maybe I didn't understand you fully, but this is what I have so far (not working). When I say it is not working, what I mean is: It will zoom (which is good), but it will first try to shift the image up or down (which is bad).

bool ImageForm::eventFilter(QObject *o, QEvent *e)
{
...
if (o == ui.graphicsView)
{
if (e->type == QEvent::Wheel)
{
QWheelEvent *q = dynamic_cast<QWheelEvent*>(e);
scaleView((double)2, -q->delta() / 240.0));
// q->accept();
return true;
}
}
}

Any ideas why it is still trying to move the picture up or down based on the wheelevent?

jpn
11th October 2006, 19:56
Just curious, what happens if you install the event filter on graphics view's viewport() as well?

forrestfsu
11th October 2006, 20:02
I just added:

ui.graphicsView->viewport()->installEventFilter(this);

This had no effect...did you want me to modify the filter code also?

forrestfsu
11th October 2006, 20:05
Haha, you're good! That worked! Thanks guys...the code that made it work was something like this:
ui.graphicsView->viewport()->installEventFilter(this);

bool ImageForm::eventFilter(QObject *o, QEvent *e)
{
...
if (o == ui.graphicsView->viewport())
{
if (e->type == QEvent::Wheel)
{
QWheelEvent *q = dynamic_cast<QWheelEvent*>(e);
scaleView((double)2, -q->delta() / 240.0));
// q->accept();
return true;
}
}
}