Results 1 to 2 of 2

Thread: pan view of QGraphicsView

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default pan view of QGraphicsView

    hi friends,

    Please help me to get the basic method of showing the panview of QGraphicsView

    on following the the forum code .
    http://qtcenter.org/wiki/index.php?t...ng_and_Zooming

    Qt Code:
    1. {
    2. QBrush m_background_brush;
    3. m_background_brush.setStyle(Qt::SolidPattern);
    4. m_background_brush.setColor(QColor(3, 24, 39));
    5. Scene->setBackgroundBrush(m_background_brush);
    6.  
    7.  
    8. //Set-up the view
    9. setSceneRect(-500, -500, 1000, 1000);
    10. }
    11.  
    12. void MyGraphicsView::SetCenter(const QPointF& centerPoint) {
    13. //Get the rectangle of the visible area in scene coords
    14. QRectF visibleArea = mapToScene(rect()).boundingRect();
    15.  
    16. //Get the scene area
    17. QRectF sceneBounds = sceneRect();
    18. double boundX, boundY, boundWidth, boundHeight;
    19.  
    20. if(centerPoint.x() >= 0)
    21. {
    22. boundX = visibleArea.width() / 2.0;
    23. boundY = visibleArea.height() / 2.0;
    24. boundWidth = (sceneBounds.width() - 2.0 * boundX);
    25. boundHeight = (sceneBounds.height() - 2.0 * boundY);
    26. } else
    27. {
    28. boundX = visibleArea.width() / 2.0;
    29. boundY = visibleArea.height() / 2.0;
    30. boundWidth = (sceneBounds.width() + 2.0 * boundX);
    31. boundHeight = (sceneBounds.height() + 2.0 * boundY);
    32. }
    33.  
    34. //The max boundary that the centerPoint can be to
    35. QRectF bounds(boundX, boundY, boundWidth, boundHeight);
    36.  
    37. if(bounds.contains(centerPoint)) {
    38.  
    39. CurrentCenterPoint = centerPoint;
    40. } else {
    41.  
    42. if(visibleArea.contains(sceneBounds)) {
    43.  
    44. CurrentCenterPoint = sceneBounds.center();
    45. } else {
    46.  
    47. CurrentCenterPoint = centerPoint;
    48.  
    49.  
    50. if(centerPoint.x() > bounds.x() + bounds.width()) {
    51. qDebug()<<"The Clamping on the min X"<<bounds <<" and the clamp:"<< centerPoint;
    52. CurrentCenterPoint.setX(bounds.x() + bounds.width());
    53. } else if(centerPoint.x() < bounds.x()) {
    54. CurrentCenterPoint.setX(bounds.x() );
    55. }
    56.  
    57. if(centerPoint.y() > bounds.y() + bounds.height()) {
    58. qDebug()<<"The Clamping on the min Y";
    59. CurrentCenterPoint.setY(bounds.y() + bounds.height());
    60. } else if(centerPoint.y() < bounds.y()) {
    61. CurrentCenterPoint.setY(bounds.y() );
    62. }
    63.  
    64. }
    65. }
    66.  
    67. qDebug() <<"Update the scrollbars:" << CurrentCenterPoint;
    68. centerOn(CurrentCenterPoint);
    69. }
    70.  
    71.  
    72. */
    73. void MyGraphicsView::mousePressEvent(QMouseEvent* event) {
    74. //For panning the view
    75. LastPanPoint = event->pos();
    76. setCursor(Qt::ClosedHandCursor);
    77. }
    78.  
    79. /**
    80.   * Handles when the mouse button is released
    81.   */
    82. void MyGraphicsView::mouseReleaseEvent(QMouseEvent* event) {
    83. setCursor(Qt::OpenHandCursor);
    84. LastPanPoint = QPoint();
    85. }
    86.  
    87. /**
    88. *Handles the mouse move event
    89. */
    90. void MyGraphicsView::mouseMoveEvent(QMouseEvent* event) {
    91. if(!LastPanPoint.isNull()) {
    92. //Get how much we panned
    93. QPointF delta = mapToScene(LastPanPoint) - mapToScene(event->pos());
    94. qDebug()<<"The Wheel Delta value:"<< delta;
    95. LastPanPoint = event->pos();
    96.  
    97. //Update the center ie. do the pan
    98. if(delta.x() < 0 )
    99. {
    100. SetCenter(GetCenter() - delta);
    101. } else
    102. SetCenter(GetCenter() + delta);
    103. }
    104. }
    105.  
    106. /**
    107.   * Zoom the view in and out.
    108.   */
    109. void MyGraphicsView::wheelEvent(QWheelEvent* event) {
    110.  
    111. //Get the position of the mouse before scaling, in scene coords
    112. QPointF pointBeforeScale(mapToScene(event->pos()));
    113.  
    114. //Get the original screen centerpoint
    115. QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());
    116.  
    117. //Scale the view ie. do the zoom
    118. double scaleFactor = 1.15; //How fast we zoom
    119. if(event->delta() > 0) {
    120. //Zoom in
    121. scale(scaleFactor, scaleFactor);
    122. } else {
    123. //Zooming out
    124. scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    125. }
    126.  
    127. //Get the position after scaling, in scene coords
    128. QPointF pointAfterScale(mapToScene(event->pos()));
    129.  
    130. //Get the offset of how the screen moved
    131. QPointF offset = pointBeforeScale - pointAfterScale;
    132.  
    133. //Adjust to the new center for correct zooming
    134. QPointF newCenter = screenCenter + offset;
    135. SetCenter(newCenter);
    136. }
    137.  
    138. void MyGraphicsView::resizeEvent(QResizeEvent* event) {
    139. QRectF visibleArea = mapToScene(rect()).boundingRect();
    140. SetCenter(visibleArea.center());
    141.  
    142. //Call the subclass resize so the scrollbars are updated correctly
    143. QGraphicsView::resizeEvent(event);
    144. }
    To copy to clipboard, switch view to plain text mode 
    but this code pans only on the positive direction of the zoom area ..

    .. if i set the sceneRect to (0, 0, 1000, 1000) panning view was clear and perfect ..
    please help me to solve this problem ..

    Thnks in advance ..
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pan view of QGraphicsView

    I have attached my code with this thread .
    Attached Files Attached Files
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. Fit In View a loaded image to QGraphicsView
    By sincnarf in forum Qt Programming
    Replies: 4
    Last Post: 11th September 2014, 08:23
  2. QWidget + QGraphicsView + OenGL view port
    By medved6 in forum Qt Programming
    Replies: 5
    Last Post: 30th March 2011, 07:55
  3. QGraphicsView: Save as PNG without displaying View?
    By SixDegrees in forum Qt Programming
    Replies: 0
    Last Post: 8th October 2010, 22:56
  4. Replies: 2
    Last Post: 31st January 2010, 02:53
  5. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04

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.