Results 1 to 4 of 4

Thread: Graphics View Panning ,zooming

  1. #1
    Join Date
    Dec 2008
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Graphics View Panning ,zooming

    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 ?

  2. #2
    Join Date
    Nov 2007
    Posts
    26
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graphics View Panning ,zooming

    For scaling you need to scale with values which will scale from your views current scale to a your desired scale. (scaling by 1,1) wont scale anything.

    For panning QGraphicsView::ScrollHandDrag is just setting the views horizontal and vertical scrollbars. You can manually set their values or alternatively centerOn(0,0) will center your view on 0,0.

    for your example If all you want to do is put the view back to the way it was originally was:

    Qt Code:
    1. centerOn(0,0)
    2. resetMatrix ()
    To copy to clipboard, switch view to plain text mode 

    should do it.

  3. #3
    Join Date
    Dec 2008
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Graphics View Panning ,zooming

    Hey thank you thats works fine

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Graphics View Panning ,zooming

    Or you can have a look at QGraphicsView::fitInView

Similar Threads

  1. Qt Coordinate System and the Graphics View Framework
    By djurodrljaca in forum Qt Programming
    Replies: 14
    Last Post: 17th February 2012, 12:19
  2. Replies: 4
    Last Post: 5th August 2008, 20:55
  3. Graphics View Event Propagation
    By pherthyl in forum Qt Programming
    Replies: 10
    Last Post: 3rd July 2008, 11:39
  4. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 08:08
  5. Adding Rectangular overlay on graphics view
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 21st November 2006, 20:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.