Hi,

I have written a small application using Graphics View.
I have drawn a circle at the (0,0) in the scene.

Now i want to do the following:
a. ZoomIn/ZoomOut, the circle should always be the center.
b. Pan around using mouse.
c. Remove all the panning/zooming related activities (if present) and restore to the default state (without any Panning/Zoom) on a click of button .

I have done points a) and b) , but once i have panned around the view , i dont know how to get back to the default view, i.e. the view should be centered at scene's (0,0) without any scaling

This is my code for view and scene class

Qt Code:
  1. main.cpp:
  2. int main(int argc, char *argv[])
  3. {
  4.  
  5. QApplication a(argc, argv);
  6. GvHmiMainWindow Gvhmi;
  7. /*~ or this way-nithya~*/
  8. /* by with out using pointers~*/
  9.  
  10.  
  11. Gvhmi.show();
  12. return a.exec();
  13.  
  14. }
  15.  
  16. mainwindow.cpp
  17. GvHmiMainWindow::GvHmiMainWindow(QWidget *parent)
  18. : QMainWindow(parent), ui(new Ui::GvHmiMainWindowClass)
  19. {
  20. ui->setupUi(this);
  21. /*~either this way-nithya~*/
  22. /* by using pointers~*/
  23. viewGv = new GvHmiView();
  24. setCentralWidget(viewGv);
  25. QObject::connect(ui->actionPan,SIGNAL(triggered ( bool ) ),this,SLOT(slotactionPan(bool)));
  26. setWindowTitle(tr("Graphics View"));
  27. }
  28. void GvHmiMainWindow::slotactionPan(bool )
  29. {
  30. QMessageBox::warning(0,"Graphics View","Pan action disabled");
  31. viewGv->enablePan(false);
  32.  
  33. }
  34.  
  35. Scene.cpp:
  36. GvHmiScene::GvHmiScene(QObject *parent):QGraphicsScene(parent)
  37. {
  38. setBackgroundBrush(Qt::blue);
  39. setItemIndexMethod(QGraphicsScene::NoIndex);
  40. setSceneRect(-30000, -30000, 60000, 60000);
  41.  
  42. }
  43.  
  44. GvHmiScene::~GvHmiScene()
  45. {
  46.  
  47.  
  48.  
  49. }
  50.  
  51.  
  52. void GvHmiScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  53. {
  54.  
  55. QPointF p = event->scenePos();
  56. qDebug("The cursor is at position %f and %f in the Scene",p.rx(),p.ry());
  57. QGraphicsScene::mouseMoveEvent(event);
  58. }
  59.  
  60. View.cpp:
  61.  
  62. GvHmiView::GvHmiView()
  63. {
  64. /*~ code added create scene inside view nithya */
  65. GvHmiScene *scene = new GvHmiScene(this);
  66.  
  67. setCacheMode(CacheBackground);
  68. setRenderHint(QPainter::Antialiasing);
  69.  
  70. // QGraphicsTextItem * item1 = scene->addText("Hello World");
  71. // item1->setPos(0,0);
  72.  
  73. QPen pen1;
  74. pen1.setColor(Qt::red);
  75.  
  76. MyItem *item3 = new MyItem((QGraphicsItem *)0, scene, 0);
  77. item3->setPos(0,0);
  78. // scene->addLine(0,0, 10,10, pen1);
  79.  
  80.  
  81.  
  82. // setAttribute (Qt::WA_SetCursor);
  83. // setCursor(Qt::CrossCursor);
  84.  
  85.  
  86. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  87. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  88.  
  89. setTransformationAnchor(QGraphicsView::AnchorViewCenter);
  90. setAlignment(Qt::AlignCenter);
  91. setResizeAnchor(QGraphicsView::AnchorViewCenter);
  92. // setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
  93.  
  94. setScene(scene);
  95. enablePan(true);
  96.  
  97. }
  98.  
  99.  
  100. GvHmiView :: ~GvHmiView()
  101. {
  102.  
  103.  
  104. }
  105.  
  106.  
  107. void GvHmiView::wheelEvent(QWheelEvent *event)
  108.  
  109. {
  110.  
  111. scaleView(pow((double)2, event->delta() / 240.0));
  112.  
  113. }
  114.  
  115.  
  116.  
  117. void GvHmiView::scaleView(qreal scaleFactor)
  118.  
  119. {
  120.  
  121. qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
  122. if (factor < 0.07 || factor > 100)
  123. return;
  124. scale(scaleFactor, scaleFactor);
  125.  
  126. }
  127.  
  128.  
  129. [B]void GvHmiView::enableDisablePan(bool enable)
  130. {
  131. if(true == enable)
  132. setDragMode(ScrollHandDrag);
  133. else
  134. {
  135. setTransformationAnchor(QGraphicsView::AnchorViewCenter);
  136. setAlignment(Qt::AlignCenter);
  137. setResizeAnchor(QGraphicsView::AnchorViewCenter);
  138.  
  139. setTransformationAnchor(QGraphicsView::AnchorViewCenter);
  140. setDragMode(NoDrag);
  141. scale(1,1);
  142. update();
  143.  
  144. }
  145.  
  146. }[/B]
To copy to clipboard, switch view to plain text mode 
GvHmiView::enableDisablePan(bool enable) is the function where i am trying to get the view to show default screen , without any zoom/pan.
Can anyone suggest what going wrong here ?