PDA

View Full Version : QGraphicsView scroll fade out



Cruz
30th September 2013, 00:17
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.



void GraphicsViewWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint mouse = event->pos();
QPoint mouseDiff = mouse - lastMouse;
lastMouse = mouse;
translate(mouseDiff.x(), mouseDiff.y());
update();
}


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.

Cruz
30th September 2013, 10:03
Ya, it was the scrollbars that interfered. After setting



setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
verticalScrollBar()->disconnect();
horizontalScrollBar()->disconnect();


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



void GraphicsViewWidget::mousePressEvent(QMouseEvent *event)
{
setCursor(Qt::ClosedHandCursor);
}

void GraphicsViewWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint mouse = event->pos();
QPoint mouseDiff = (mouse - lastMouse);
lastMouse = mouse;
QPointF mappedMouseDiff = mapToScene(mouseDiff) - mapToScene(0,0);

if (event->buttons() & (Qt::LeftButton | Qt::RightButton | Qt::MiddleButton))
{
translate(mappedMouseDiff.x(), mappedMouseDiff.y());
}
}

void GraphicsViewWidget::mouseReleaseEvent(QMouseEvent *event)
{
unsetCursor();
}


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. :)