Results 1 to 6 of 6

Thread: "Smooth" Scrolling, QGraphicsView

  1. #1
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default "Smooth" Scrolling, QGraphicsView

    Hey,

    I've a problem:

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

    Qt Code:
    1. if(!scene()->collidingItems(this).isEmpty())
    2. {
    3. emit newKollision(positiony);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. VerticalScrollBar = this->verticalScrollBar();
    2. QObject::connect(mouse, SIGNAL(newKollision(int)), this, SLOT(ScrollBarSetValue(int)));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void GraphWidget::ScrollBarSetValue(int value)
    2. {
    3. VerticalScrollBar->setValue(value-400);
    4. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: "Smooth" Scrolling, QGraphicsView

    Copy the code from
    Qt Code:
    1. void QGraphicsView::centerOn ( const QGraphicsItem * item )
    To copy to clipboard, switch view to plain text mode 
    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().

  3. #3
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "Smooth" Scrolling, QGraphicsView

    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

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: "Smooth" Scrolling, QGraphicsView

    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.

  5. #5
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "Smooth" Scrolling, QGraphicsView

    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

  6. #6
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: "Smooth" Scrolling, QGraphicsView

    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.

Similar Threads

  1. Replies: 3
    Last Post: 15th February 2010, 17:27
  2. Replies: 1
    Last Post: 15th June 2009, 17:28
  3. Replies: 3
    Last Post: 8th July 2008, 19:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

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.