PDA

View Full Version : "Smooth" Scrolling, QGraphicsView



nearlyNERD
22nd February 2010, 17:17
Hey,

I've a problem:

Every time, my "main"-item hits another, my QGraphicsView should "scroll" to / center on my main-item: (y-coord)


if(!scene()->collidingItems(this).isEmpty())
{
emit newKollision(positiony);
}


VerticalScrollBar = this->verticalScrollBar();
QObject::connect(mouse, SIGNAL(newKollision(int)), this, SLOT(ScrollBarSetValue(int)));



void GraphWidget::ScrollBarSetValue(int value)
{
VerticalScrollBar->setValue(value-400);
}
VerticalScrollBar->scroll(...., ...) doesn't work -> there appears a gray background, when I call this function.

This works well, but there is no smooth scrolling to the y-position of the item, of course...

So, has anybody an idea, how to implement smooth scrolling on a QGraphicsView, if I want to set my y-value in the application (no MouseEvent)

Thanks

spud
23rd February 2010, 17:45
Copy the code from

void QGraphicsView::centerOn ( const QGraphicsItem * item )
just avoid changing the x value.

And you should probably get used to working with scene coordinates as opposed to window coordinates. There is no need to take the roundabout tour over the scrollbars, with their limited resolution when you can simply call QGraphicsView::translate().

nearlyNERD
25th February 2010, 13:15
Thanks for the answer, but if I call "centerOn", then there is also no "smooth" scrolling, is it?

I also found centerOn, but I thought with this function it is more complicated to scroll to the item smootlhly...

I would love a function like: "QScrollBar::scrollTo(int x, int y, int time_in_ms)" or "QGraphicsView::scrollToItem( const QGraphicsItem * item, int time_in_ms)"...


nearlyNERD

spud
25th February 2010, 13:46
Sorry, I misunderstood what you meant by "smooth". You are going to have to use a timer for that, or alternatively QTimeLine.
I recommend calling translate() instead of using the scrollbars, because of the integer precision issue.

nearlyNERD
25th February 2010, 16:50
Okay, thanks.

So I should connect the signal, which is emitted, when the main-item collides with another one to a slot, which translates the QGraphicsView?

So, there are 3 Questions:

When I call "QGraphicsView::translate()", do all the items of the view move e.g. 100 pixel down?

And:
Is it easier/better to use the animation-framework of qt 4.6 (I have to upgrade from qt 4.5) instead of a QTimeLine or QTimer, because the animation-framework uses also QTimeLine?

If I do it this way, then I have to translate/move all items of the QGraphicsView simultaneous e.g. 150 pixel down to have the same effect as it would be, when I scroll smoothly, or isn't that right? And does the pixmap, I use as background, have to be also animated?

Sry, 'bout all this questions, but I don't know really how to do smooth scrolling to an item...

thanks in advance.

nearlyNERD

spud
25th February 2010, 17:18
When I call "QGraphicsView::translate()", do all the items of the view move e.g. 100 pixel down?
The visual effect will be of all items and the background moving in the reverse direction you specified in translate. Remember you are not moving the scene, but the current view port, which means the answer to your third question is no.

And:
Is it easier/better to use the animation-framework of qt 4.6 (I have to upgrade from qt 4.5) instead of a QTimeLine or QTimer, because the animation-framework uses also QTimeLine?
QTimeLine should be perfectly enough for your needs.