I am facing some real weird behavior with rubberband drag and wheelevent. If I disable the other events (mousepress, mouse move and mouse release), the wheelevent works properly and zooms my screen as it should have done.

However, if I add the other three events, the wheel event doesnt zoom with mouse position at center anymore. It zooms, but on its own way, irrespective of the mouse position.

Here are the sample codes.

Kindly help.

Qt Code:
  1. class MyGraphics : public QGraphicsView{
  2.  
  3. public :
  4. MyGraphics(QWidget *parent = NULL);
  5.  
  6. public slots:
  7. void zoomIn() { scale(1.2, 1.2); }
  8. void zoomOut() { scale(1 / 1.2, 1 / 1.2); }
  9.  
  10. protected :
  11. QRubberBand *rubberBand;
  12. QPoint origin;
  13. QPointF InitialCenterPoint;
  14. QPointF CurrentCenterPoint;
  15. QPoint rubberBandOrigin;
  16. bool rubberBandActive;
  17. QPoint LastPanPoint;
  18. int _numScheduledScalings;
  19.  
  20. //if I uncomment these three event handlers, the wheelevent doesnt zoom properly!
  21.  
  22. // virtual void mousePressEvent(QMouseEvent *event);
  23. // virtual void mouseMoveEvent(QMouseEvent *event);
  24. // virtual void mouseReleaseEvent(QMouseEvent *event);
  25. virtual void wheelEvent(QWheelEvent *);
  26. virtual void resizeEvent(QResizeEvent *event);
  27.  
  28. };
  29.  
  30.  
  31. MyGraphics::MyGraphics(QWidget *parent) : QGraphicsView(parent){
  32. setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  33.  
  34. this->installEventFilter(this);
  35.  
  36. this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
  37. this->setResizeAnchor( QGraphicsView::AnchorUnderMouse );
  38. QGraphicsScene *graphScene = new QGraphicsScene(this);
  39.  
  40. this->setScene(graphScene);
  41.  
  42. // QDebug << "Left " << sceneRect().
  43. QGraphicsItem* pEllipse = graphScene->addEllipse(0,100,50,50);
  44. QGraphicsItem* pRect = graphScene->addRect(200,100,50,50);
  45.  
  46. pEllipse->setFlag(QGraphicsItem::ItemIsSelectable, true);
  47. pRect->setFlag(QGraphicsItem::ItemIsSelectable, true);
  48.  
  49. setSceneRect(0, 0, 1000, 1000);
  50. rubberBandOrigin = QPoint(0,0);
  51. }
  52.  
  53. void MyGraphics::wheelEvent(QWheelEvent *event){
  54.  
  55. double scaleFactor = 1.2;
  56.  
  57. if(event->delta() > 0){
  58. //Zoom in
  59. this->zoomIn();
  60. //this->scale(scaleFactor,scaleFactor);
  61. } else {
  62. this->zoomOut();
  63. //Zoom Out
  64. //this->scale(1.0/scaleFactor, 1.0/scaleFactor);
  65. }
  66.  
  67. //QGraphicsView::wheelEvent(event);
  68. }
  69.  
  70. void MyGraphics::resizeEvent(QResizeEvent* event) {
  71. //Get the rectangle of the visible area in scene coords
  72. QRectF visibleArea = this->mapToScene(rect()).boundingRect();
  73.  
  74. this->fitInView(0,0,visibleArea.width(), visibleArea.height(),Qt::KeepAspectRatio);
  75.  
  76. //Call the subclass resize so the scrollbars are updated correctly
  77. QGraphicsView::resizeEvent(event);
  78.  
  79. }
  80.  
  81. /*
  82. void MyGraphics::mousePressEvent(QMouseEvent *event)
  83. {
  84.   if (event->button() == Qt::MiddleButton) {
  85.   rubberBandOrigin = event->pos();
  86.   rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  87.   rubberBand->setGeometry(event->x(),event->y(),0, 0);
  88.   rubberBand->show();
  89.   rubberBandActive = true;
  90.   }
  91.   if(event->button() == Qt::LeftButton){
  92.   LastPanPoint = event->pos();
  93.   }
  94.  
  95. }
  96.  
  97. void MyGraphics::mouseMoveEvent(QMouseEvent *event)
  98. {
  99.   if (event->buttons() == Qt::MiddleButton && rubberBandActive == true){
  100.   rubberBand->resize( event->x()-rubberBandOrigin.x(), event->y()-rubberBandOrigin.y() );
  101.   }
  102.   else{
  103.   if(!LastPanPoint.isNull()) {
  104.   //Get how much we panned
  105.   QGraphicsView * view = static_cast<QGraphicsView *>(this);
  106.  
  107.   QPointF delta = view->mapToScene(LastPanPoint) - view->mapToScene(event->pos());
  108.   LastPanPoint = event->pos();
  109.   }
  110.   }
  111. }
  112.  
  113. void MyGraphics::mouseReleaseEvent(QMouseEvent *event)
  114. {
  115.   if (event->button() == Qt::MiddleButton){
  116.   QGraphicsView * view = static_cast<QGraphicsView *>(this);
  117.  
  118.   QPoint rubberBandEnd = event->pos();
  119.  
  120.   QRectF zoomRectInScene = QRectF(view->mapToScene(rubberBandOrigin), view->mapToScene(rubberBandEnd));
  121.   QPointF center = zoomRectInScene.center();
  122.  
  123.   view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
  124.   rubberBandActive = false;
  125.   delete rubberBand;
  126.   }
  127.   else{
  128.   LastPanPoint = QPoint();
  129.   }
  130. }
  131. */
To copy to clipboard, switch view to plain text mode