Results 1 to 5 of 5

Thread: QGrapghicsView updating

  1. #1
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGrapghicsView updating

    Here is a simple re-implementation of mouse events for a QGraphicsView:

    Qt Code:
    1. void kadeView::mousePressEvent (QMouseEvent * event )
    2. {
    3. if (event->button() == Qt::MidButton) //pan
    4. {
    5. isPanning=1;
    6. saved_cursor=viewport()->cursor();
    7. setCursor(Qt::ClosedHandCursor);
    8. mouse_pos=mapToScene(event->pos());
    9. event->accept();
    10. return;
    11. };
    12. QGraphicsView::mousePressEvent(event);
    13. };
    14.  
    15. void kadeView::mouseMoveEvent ( QMouseEvent * event )
    16. {
    17. if(isPanning==1)
    18. {
    19. QPointF delta(mouse_pos-mapToScene(event->pos()));
    20. QRectF r1(sceneRect());
    21. r1.translate(delta.x(),delta.y());
    22. setSceneRect(r1);
    23. //translate(delta.x(),delta.y());
    24. //ensureVisible(pan_rect.x()-line.dx(),pan_rect.y()-line.dy(),pan_rect.width(),pan_rect.height());
    25. //centerOn(pan_rect.center().x()-line.dx(),pan_rect.center().y()-line.dy());
    26. event->accept();
    27. return;
    28. };
    29. QGraphicsView::mouseMoveEvent(event);
    30. };
    31.  
    32. void kadeView::mouseReleaseEvent ( QMouseEvent * event )
    33. {
    34. if(isPanning==1)
    35. {
    36. isPanning=0;
    37. setCursor(saved_cursor);
    38. event->accept();
    39. return;
    40. };
    41. QGraphicsView::mouseReleaseEvent(event);
    42. };
    To copy to clipboard, switch view to plain text mode 

    It pans the view on middle mouse click and drag.
    Can someone explain me why the only command that works is the setSceneRect() in mouseMoveEvent, and not the commented ones?
    If I uncomment and replace setSceneRect with any of the translate(), centerOn(), ensureVisible() nothing is happening in the view...

    Thank you in advance

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGrapghicsView updating

    Why are you changing the scene rect?
    Don't you need QGraphicsView::fitInView instead?

    Be carefut that QGraphicsView::sceneRect does not returns the visible rect in the view.

    I have same needs and I didn't find better to get it than :
    Qt Code:
    1. mapToScene(viewport()->rect())
    To copy to clipboard, switch view to plain text mode 

    How is update your variable pan_rect?

    Be aware than when you code will work, the return coordinates of
    Qt Code:
    1. mapToScene(event->pos())
    To copy to clipboard, switch view to plain text mode 
    will have same value in mousePress and mouseMove,
    by definition of the pan : the pixel coordinates change, but scene coordinates are attach to the mouse!!

    And last but not least : are you aware of QGraphicsView::setDragMode with QGraphicsView::ScrollHandDrag?

  3. #3
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGrapghicsView updating

    The problem is not the coordinates. All the coords are checked to be fine.

    The problem is that the calls
    translate()
    or
    centerOn()
    didn't move the view...
    For example If I put in mouseMoveEvent the call translate(10,10) it will not move the view.
    The only call that I have found to move the view is the setSceneRect(), although in documentation it implies that this is not the view piece of the scene, it's the entire scene. In practise it seems to be the viewable rect of the scene.

    Am I missing something about the QGraphicsView structure?
    When I call translate, rotate etc etc , view is not updated? Why? And how can I do it?

    I am at the begining of a project. It's my first use of QGraphicsView/Scene and I have to clear out all the above, before I move on with my work...

  4. #4
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGrapghicsView updating

    Ok I think I see what is happening

    You cannot set the view of the scene outside the sceneRect.
    The result of the view changes will be block to the closest part of the rect scene.

    That is why, when you change the scene rect, you can move.

    Try to set your scene Rect with big values at initialization and dont call sceneRect in mouse event

  5. #5
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGrapghicsView updating

    You 're great!
    I will try it right away!
    Thank you, I lost 1 or 2 days just for that :-)

Similar Threads

  1. SqlTableModel not updating AbstractProxyModel
    By akiross in forum Qt Programming
    Replies: 7
    Last Post: 13th November 2008, 17:51
  2. Updating Status Bar Icon
    By nbkhwjm in forum Qt Programming
    Replies: 1
    Last Post: 8th October 2008, 05:22
  3. Updating QTreeView with custom model
    By supergillis in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 08:01
  4. Treeview scrollbar not updating when inserting rows
    By Khal Drogo in forum Qt Programming
    Replies: 11
    Last Post: 29th November 2007, 14:13
  5. Problem with QScrollArea updating from 4.0.1 to 4.1.0
    By SkripT in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2006, 23:35

Tags for this Thread

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.