PDA

View Full Version : WheelEvent on just 'GraphicsView' and not in whole window



gaganbm
7th February 2013, 11:43
Hello,

I have a Main Window and within it I have a bunch of UI elements like tab browsers, graphview and so on. It has been created using Qt Creator and not by explicitily making class objects for each component and adding it to the Main Window.

I want to add wheelEvent for zoom in and zoom out actions on the GraphView section. But it effects the whole window. That is, I want the zoom-in and zoom-out to happen only when the WheelEvent is done in the graph view. But at present, anywhere in the main window I do the scrolling, the zooming takes place.

How do I restrict the WheelEvent only to the graphview ? One way I thought was to capture the mouse position somehow and check if it is within the GraphView and then proceed. Secondly, may be adding the graphview as a separate class and instantiating it in the Main Window, and deleting it from the Qt Creator ui form.

Please suggest how to achieve this.

Thank you.

wysota
7th February 2013, 11:45
Either promote your graphics view to a custom class and implement the event in that class or install an event filter on the graphics view and intercept wheel events in your window class.

Santosh Reddy
7th February 2013, 11:45
How are you currently doing the zoom ?

gaganbm
7th February 2013, 12:19
I got this piece of code :



void MainWindow::wheelEvent(QWheelEvent* event) {

//Get the position of the mouse before scaling, in scene coords
QPointF pointBeforeScale(ui->graphWidget->mapToScene(event->pos()));

//Get the original screen centerpoint
QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());

//Scale the view ie. do the zoom
double scaleFactor = 1.15; //How fast we zoom
if(event->delta() > 0) {
//Zoom in
ui->graphWidget->scale(scaleFactor, scaleFactor);
} else {
//Zooming out
ui->graphWidget->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}

//Get the position after scaling, in scene coords
QPointF pointAfterScale(ui->graphWidget->mapToScene(event->pos()));

//Get the offset of how the screen moved
QPointF offset = pointBeforeScale - pointAfterScale;

//Adjust to the new center for correct zooming
QPointF newCenter = screenCenter + offset;
SetCenter(newCenter);
}



As you can see, the GraphicsView is ui->graphWidget . But it is a part of the mainwindow, and not built programattically. It is part of the ui form of the main window.

Santosh Reddy
7th February 2013, 13:09
Nice, now move this implementation to a event filter object, and install the event filter on QGraphicsView