void MapGraphicView::wheelEvent(QWheelEvent *event){ if (event->delta() > 0) scale (1.1,1.1); else scale (1/1.1,1/1.1); event->accept();}
#include <QGraphicsView>class MapGraphicView: public QGraphicsView{ Q_OBJECTpublic: MapGraphicView(QWidget * pWgt = 0); MapGraphicView(QGraphicsScene * pScene, QWidget * pWgt = 0);protected: QPointF center; // The center of visible area QPointF fixedPoint; // Fixation point (for scrolling) void setCenter (const QPointF& centerPoint); QPointF getCenter (); virtual void mousePressEvent(QMouseEvent *event); virtual void mouseReleaseEvent(QMouseEvent *event); virtual void mouseMoveEvent(QMouseEvent *event); virtual void wheelEvent(QWheelEvent *event); virtual void resizeEvent (QResizeEvent *event);};MapGraphicView::MapGraphicView(QWidget * pWgt): QGraphicsView (pWgt){ fixedPoint = QPoint ();}MapGraphicView::MapGraphicView(QGraphicsScene * pScene, QWidget * pWgt): QGraphicsView (pScene, pWgt){ fixedPoint = QPoint ();}void MapGraphicView::setCenter(const QPointF ¢erPoint){ QRectF visibleArea = mapToScene(rect()).boundingRect(); QRectF sceneBounds = sceneRect(); double boundX = visibleArea.width()/2.0; double boundY = visibleArea.height()/2.0; double boundWidth = sceneBounds.width() - visibleArea.width(); double boundHeight = sceneBounds.height() - visibleArea.height(); QRectF bounds (boundX, boundY, boundWidth, boundHeight); center = centerPoint; if (!bounds.contains(centerPoint)) { if (visibleArea.contains(sceneBounds)) { center = sceneBounds.center(); } else { if (centerPoint.x() > bounds.x() + bounds.width()) center.setX(bounds.x() + bounds.width()); else if (center.x() < bounds.x()) center.setX(bounds.x()); if (centerPoint.y() > bounds.y() + bounds.height()) center.setY(bounds.y() + bounds.height()); else if (centerPoint.y() < bounds.y()) center.setY(bounds.y()); } centerOn(center); }}QPointF MapGraphicView::getCenter(){ return center;}void MapGraphicView::mousePressEvent(QMouseEvent *event){ fixedPoint = event->pos();}void MapGraphicView::mouseReleaseEvent(QMouseEvent *event){ fixedPoint = QPoint ();}void MapGraphicView::mouseMoveEvent(QMouseEvent *event){ if (!fixedPoint.isNull()) { QPointF offset = mapToScene(fixedPoint.toPoint()) - mapToScene(event->pos()); fixedPoint = event->pos(); setCenter(getCenter() + offset); }}void MapGraphicView::wheelEvent(QWheelEvent *event){ QPointF pointBeforeScale (mapToScene(event->pos())); QPointF visibleCenter = getCenter(); double scaleFactor = 1.2; if (event->delta() > 0) scale(scaleFactor, scaleFactor); else scale(1.0/scaleFactor, 1.0/scaleFactor); QPointF pointAfterScale(mapToScene(event->pos())); QPointF offset = pointBeforeScale - pointAfterScale; QPointF newCenter = visibleCenter+offset; setCenter(newCenter); event->accept();}void MapGraphicView::resizeEvent(QResizeEvent *event){ QRectF visibleArea = mapToScene(rect()).boundingRect(); setCenter(visibleArea.center()); QGraphicsView::resizeEvent(event);}
C++ (Qt)QGraphicsItem::setFrag( QGraphicsItem::ItemIgnoresTransformations );