Results 1 to 4 of 4

Thread: How to change the content's position in viewport?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to change the content's position in viewport?

    Hi all,
    I am a new Qt programmer, and now, I have a question need your help.

    I want to zoom in the picture which showing in QGraphicsView. The work flow like below:
    press down right button and get the mouse position(mZoomStartPoint) -> move the mouse and draw a rectangle
    via QRubberBand class ->release the mouse and get the mouse position(to_point).
    and then, scale the picture and show the items which are covered by the rectangle center on the viewport.
    the Qt code are:

    //MyGraphicsView inherites QGraphicsView

    Qt Code:
    1. void MyGraphicsView::mousePressEvent(QMouseEvent *e)
    2. {
    3. if(e->button() == Qt::RightButton)
    4. {
    5. mpMouseEvent = e;
    6. setZoomBeginPosition();
    7. }
    8. }
    9.  
    10. void MyGraphicsView::mouseMoveEvent(QMouseEvent *e)
    11. {
    12. if(e->button() == Qt::RightButton)
    13. {
    14. setZoomMoveTo();
    15. }
    16. }
    17.  
    18.  
    19. void MyGraphicsView::mouseReleaseEvent(QMouseEvent *e)
    20. {
    21. if(e->button() == Qt::RightButton)
    22. {
    23. setZoomFinish();
    24. }
    25. }
    26.  
    27. void MyGraphicsView::setZoomBeginPosition()
    28. {
    29. mZoomStartPoint = mpMouseEvent->pos();
    30. mpRubberBand->setGeometry(QRect(mZoomStartPoint, QSize()));
    31. mpRubberBand->show();
    32. }
    33.  
    34. void MyGraphicsView::setZoomMoveTo()
    35. {
    36. QPoint to_point = mpMouseEvent->pos();
    37. if(mpRubberBand)
    38. mpRubberBand->setGeometry(QRect(mZoomStartPoint, to_point).mormalized());
    39. }
    40.  
    41. void MyGraphicsView::setZoomFinish()
    42. {
    43. Qpoint to_point = mpMouseEvent->pos();
    44. int left_x = to_point.x()<mZoomStartPoint.x() ? to_point.x() : mZoomStartPoint.x();
    45. int right_x = to_point.x()>mZoomStartPoint.x() ? to_point.x() : mZoomStartPoint.x();
    46. int top_y = to_point.y()<mZoomStartPoint.y() ? to_point.y() : mZoomStartPoint.y();
    47. int bottom_y = to_point.y()<mZoomStartPoint.y() ? to_point.y() : mZoomStartPoint.y();
    48.  
    49. if(abs(left_x - right_x)<10 || abs(top_y - bottom_y) < 10)
    50. return;
    51.  
    52. float x_scale = (viewport()->width()) / (right_x - left_x);
    53. float y_scale = (viewport()->height()) / (bottom_y - top_y);
    54. float scale = x_scale < y_scale ? x_scale : y_scale;
    55. QMatrix m = matrix();
    56. m.scale(scale);
    57. setMatrix(m);
    58. }
    To copy to clipboard, switch view to plain text mode 

    When I test, the items which covered by Rectangle can not show center on viewport , I must srcoll the slider to show the items
    and zoom fit the picture, then test again , the items will be show.

    So, would you like help me ? Thanks a lot !
    Last edited by wysota; 24th March 2009 at 07:28.

Similar Threads

  1. how to change the position of menu of QMainWindow
    By yxmaomao in forum Qt Programming
    Replies: 11
    Last Post: 17th September 2016, 16:12
  2. Replies: 1
    Last Post: 13th September 2008, 11:00
  3. Replies: 1
    Last Post: 3rd October 2007, 07:34
  4. QGraphicsTextItem: handling cursor position change
    By Angelo Moriconi in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2007, 10:42
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 01:34

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
  •  
Qt is a trademark of The Qt Company.