I got this piece of code :
//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);
}
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);
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks