Results 1 to 4 of 4

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

  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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Use QGraphicsView::fitInView() instead of manually calculating the matrix.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    aaron (24th March 2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Ok , I will try it . thanks!

  5. #4
    Join Date
    Mar 2009
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Dear wysota,
    I have solve this problem by your idea, thanks again for your help.

    By the way, if you meet the same issue in your program and the current picture has been scaled, please set the second param of the QGraphicsView::fitInview(...) to Qt::KeepAspectRatio.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.