Results 1 to 11 of 11

Thread: QGraphicsView performance problems

  1. #1
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QGraphicsView performance problems

    Hello,

    I am trying to write a roundabout simulator. I have a class inheriting QGraphicsView. Here's the initialization from the constructor:
    Qt Code:
    1. _scene = new QGraphicsScene;
    2. _scene->setSceneRect(-1000, -1000, 2000, 2000);
    3. _scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    4.  
    5. setScene(_scene);
    6. setCacheMode(QGraphicsView::CacheBackground);
    7. setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    8. setDragMode(QGraphicsView::ScrollHandDrag);
    9. setOptimizationFlag(DontAdjustForAntialiasing);
    To copy to clipboard, switch view to plain text mode 
    I tried adding some mice from Colliding Mice example. When there are more then twenty (or so) of them, performance drops. I commented out any of my roundabout-drawing code, so now it's just mice on a green background.
    I tried simplifying the mouse:aint() and mouse::advance() functions, but that didn't seem to do the trick.

    What is the reason behind the slowdown? I wanted to have many more cars moving (and detecting collisions) then twenty should I use different approach then GraphicsView?

  2. #2
    Join Date
    Nov 2009
    Location
    Sacramento, CA
    Posts
    24
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView performance problems

    You can try FullViewportUpdate and set the viewport to use opengl.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsView performance problems

    And maybe turn off indexing if you don't need it

  4. #4
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView performance problems

    Thank you for quick responses. I have already disabled indexing as you can see in the code in my first post.

    Switching to OpenGL helped a little: now slowdown is noticible at about 54 mice running around. So it's better, but still not as good as I hoped it would be. Do you have any other suggestions?

    If you want to reproduce my problem, the simpliest way is to open the Colliding Mice example and change MouseCount to a higher value.

  5. #5
    Join Date
    Nov 2009
    Location
    Sacramento, CA
    Posts
    24
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView performance problems

    This is just a guess cause I don't know what qt does under the hood, but if you turn indexing off and try to do collision detection on that many objects with a naive approch, you will see a performance problem.

  6. #6
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView performance problems

    I'm afraid it has nothing to do with collision detection. I commented out code from mouse::advance that was checking for collisions, leaving only basic movement, and the slowdown still occurs the same way.

  7. #7
    Join Date
    May 2010
    Location
    Russia
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView performance problems

    Hi, My suggestion: if there is a problem with the perfomance rendering of graphics objects, try to use QtOpengl in Double Buffering mode.

  8. #8
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView performance problems

    I added:
    Qt Code:
    1. qglwidget = new QGLWidget;
    2. qglwidget->context()->format().setDoubleBuffer(true);
    3. setViewport(qglwidget);
    To copy to clipboard, switch view to plain text mode 
    to the initalization code given above, but without any positive outcome.
    (I hope I did it correctly)

  9. #9
    Join Date
    May 2010
    Location
    Russia
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView performance problems

    Quote Originally Posted by MarPan View Post
    I added:
    Qt Code:
    1. qglwidget = new QGLWidget;
    2. qglwidget->context()->format().setDoubleBuffer(true);
    3. setViewport(qglwidget);
    To copy to clipboard, switch view to plain text mode 
    to the initalization code given above, but without any positive outcome.
    (I hope I did it correctly)
    I hope, In this case you don't use QGraphicsScene and QGraphicsView for painting object's? you should use
    Qt Code:
    1. void GLWidget::paintEvent(QPaintEvent *event)
    To copy to clipboard, switch view to plain text mode 
    for manually painting objects (without QGraphicsScene, QGraphicsView classes).
    Rendering with using only QtOpengGl must give more efficiency then using graphics scene.
    Last edited by xray2000; 2nd July 2010 at 07:02.

  10. #10
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView performance problems

    Firstly, I may have misinformed you. When I wrote
    I commented out code from mouse::advance that was checking for collisions
    I was right, but there was also one line in paint(), that was resposible for color of ears if there was a collision.

    So right now, instead of drawing a mouse, I load a pixmap from disk (it's just a 25x50 monocolored rectangle) and store a pointer inside a mouse class. In the paint function I have
    Qt Code:
    1. painter->setClipRect( option->exposedRect );
    2. pixmap->fill(scene()->collidingItems(this).isEmpty() ? Qt::cyan : Qt::red);
    3. painter->drawPixmap(pixmap->rect(), *pixmap, pixmap->rect());
    To copy to clipboard, switch view to plain text mode 
    With code like this, the maximum is about 60 "cars". When I comment the middle line, I can have over 200 without significant slowdown.

    So it apperas it's collision after all. But documentation says I can have "millions of items on the scene". Am I doing something wrong?
    Of course I set the ItemIndexMethod back to QGraphicsScene::BspTreeIndex. I also left viewport set to opengl with double buffering on, since it helped a little (though I don't know why).

    And I don't really want to give up on qgraphicsview. The reason I chose it was because of it's collision tools.


    EDIT: I just have read an interesting blog entry: http://labs.trolltech.com/blogs/2009...een-rendering/. Andreas also tried improving performance of colliding mice and says
    (the bottleneck of the example becomes the collision-detection)
    . Does that mean that I cannot go any further than that?
    Last edited by MarPan; 2nd July 2010 at 10:42. Reason: updated contents

  11. #11
    Join Date
    Aug 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: QGraphicsView performance problems

    Sorry to bump an oldish thread, but I've noticed similar performance issues with QGraphicsScene and collision detection, particularly when running on my Nokia 5800 (Symbian).

    I have written a simple breakout clone. There are around 27 items in the scene. When I call QGraphicsScene::collidingItems once per frame (to check for collisions between ball and blocks), performance is fine.

    I then added a bonus where the player could shoot bullets, and for each bullet, there was an additional call to QGraphicsScene::collidingItems. There was a noticable slowdown with each bullet added, with it slowing to a crawl even with only 2-3 bullets!

    I fixed the problem by writing my own spatial indexing method, implementing a 2D spatial grid. Now sure, obviously I realise that an indexing method written specifically for my game will do better than a generic indexing method, but I don't understand why it's so slow. The documentation says "One of QGraphicsScene's greatest strengths is its ability to efficiently determine the location of items. Even with millions of items on the scene, the items() functions can determine the location of an item within few milliseconds."

    My blocks are static, so I don't think that's a problem for the BSP. Also I note that things seem to run just as slowly with BSP indexing, compared with indexing disabled.

    I don't know if this is normal behaviour, or we're doing something wrong?

Similar Threads

  1. QgraphicsView performance
    By nileshsince1980 in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2010, 11:54
  2. again QGraphicsView performance
    By medved6 in forum Qt Programming
    Replies: 11
    Last Post: 21st December 2009, 21:11
  3. QGraphicsView performance in 4.6
    By Lodorot in forum Qt Programming
    Replies: 2
    Last Post: 20th September 2009, 23:09
  4. QGraphicsView performance
    By Halabund in forum Newbie
    Replies: 3
    Last Post: 17th April 2009, 10:12
  5. QGraphicsScene/QGraphicsView performance problems
    By bnilsson in forum Qt Programming
    Replies: 71
    Last Post: 28th January 2008, 12:08

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.