Results 1 to 2 of 2

Thread: QGraphicsView scroll fade out

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QGraphicsView scroll fade out

    Hello!

    With the QGraphicsView framework I want to implement a scroll fade out feature. That means that I drag the scene with the mouse just like in the ScrollHandDrag mode, but when I release the button it shouldn't stop abruptly, but keep scrolling and slow down to come to a stop. I would love to build upon the existing ScrollHandDrag mode, because it does its job really well. It scrolls smoothly and stops at the scene border. I have no idea how to trigger it programmatically though.

    Question one: Is there a way I can reuse the ScrollHandDrag mode programmatically?

    Alright, so I thought I'll just override the mouse events and e.g. use translate() in the mouseMoveEvent like so.

    Qt Code:
    1. void GraphicsViewWidget::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. QPoint mouse = event->pos();
    4. QPoint mouseDiff = mouse - lastMouse;
    5. lastMouse = mouse;
    6. translate(mouseDiff.x(), mouseDiff.y());
    7. update();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Nope, it doesn't do much. The scene barely moves by a pixel. Even if I use it with hard coded values, say translate(100,100) the scene does not move. Apparently, setting any QTransform with setTransform() seems to have no effect.

    Question two: Why doesn't any custom view transformation seems to work for me?


    When I replace translate() with scroll(), then the scene does move, but it's very jittery. And also it doesn't stop at the scene border.
    To avoid the jitter, I could remember the transform when the mouse was clicked and then calculate a new transform that is translated by the entire length of what the mouse has moved since the click was placed. But I don't see anything like a "scrollTo()" interface to go with that approach.

    Question three: How do I do it right?

    I don't want the scroll bars at all btw. I just keep them hidden and hope that they don't interfere.

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scroll fade out

    Ya, it was the scrollbars that interfered. After setting

    Qt Code:
    1. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    2. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    3. verticalScrollBar()->disconnect();
    4. horizontalScrollBar()->disconnect();
    To copy to clipboard, switch view to plain text mode 

    in the constructor, now I can scroll the view properly when dragging it with the mouse. This is the reimplementation of the ScrollHandDragMode:

    Qt Code:
    1. void GraphicsViewWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. setCursor(Qt::ClosedHandCursor);
    4. }
    5.  
    6. void GraphicsViewWidget::mouseMoveEvent(QMouseEvent *event)
    7. {
    8. QPoint mouse = event->pos();
    9. QPoint mouseDiff = (mouse - lastMouse);
    10. lastMouse = mouse;
    11. QPointF mappedMouseDiff = mapToScene(mouseDiff) - mapToScene(0,0);
    12.  
    13. if (event->buttons() & (Qt::LeftButton | Qt::RightButton | Qt::MiddleButton))
    14. {
    15. translate(mappedMouseDiff.x(), mappedMouseDiff.y());
    16. }
    17. }
    18.  
    19. void GraphicsViewWidget::mouseReleaseEvent(QMouseEvent *event)
    20. {
    21. unsetCursor();
    22. }
    To copy to clipboard, switch view to plain text mode 

    It scrolls properly no matter how the scene is scaled. It doesn't include the scroll fade out feature yet, it will remain my secret.

Similar Threads

  1. Replies: 1
    Last Post: 20th July 2010, 12:16
  2. QGraphicsView scroll bars disappears
    By jano_alex_es in forum Qt Programming
    Replies: 4
    Last Post: 25th November 2009, 14:17
  3. QGraphicsView without scroll bar
    By sanjayshelke in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 12:17
  4. Replies: 1
    Last Post: 11th June 2009, 05:49
  5. QGraphicsView : scroll on drag
    By kghose in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2008, 21:50

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.