QGraphicsView*view;QGraphicsScene*scene;// current mouse position in the sceneQPointFmouse_pos;// diameter of the magnifierdoublemagnifier_size=100.0;// scale factor inside magnifierdoublemagnify_factor;view->setScene(scene);
1. grab QGraphicsView
// after adding all contents to scene, fit in view to ensure entire scene is displayedview->fitInView(view->sceneRect(),Qt::KeepAspectRatio);// grab whole viewQPixmappix=view->grab();
2. setup magnifier
// set pix as the texture of brush QBrushbrush(pix);// set position of magnifier as a circle with current mouse position as its centerQGraphicsEllipseItem*magnifier=newQGraphicsEllipseItem(mouse_pos.x()-magnifier_size/2,mouse_pos.y()-magnifier_size/2,magnifier_size,magnifier_size);// get mouse position in viewQPointmouse_pos_view=view->mapFromScene(mouse_pos);// get position of scene origin in viewQPointorigin_view=view->mapFromScene(0,0);// calculate the ratio: distance_in_scene / distance_in_viewdoubleview_scale_ratio=(mouse_pos.x()-0)/(mouse_pos_view.x()-origin_view.x());/* the transformation matrix should work like this
T mul mouse_pos_view = mouse_pos
T mul origin_view = (0, 0)
scale should be magnify_factor * view_scale_ratio,
then calculate the translations tx and ty
*/doublescale=magnify_factor*view_scale_ratio;QTransfromtransform(scale,0.0,0.0,0.0,scale,0.0,tx,ty,1.0);brush.setTransform(transform);magnifier.setBrush(brush);// if already added to scene, do removeItem and then addItemscene->addItem(magnifier);