Results 1 to 6 of 6

Thread: Problem with QGraphicsView performance

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Problem with QGraphicsView performance

    Hello,

    I have a QGraphicsScene (800X300, 96pp) and I'm having performance problems when I try to implement a background image in a QGraphisScene.

    There are not so many items, about ten. But it's really slow. Actually, it's going slower and slower. When the application is running about half minute it takes abut two seconds to react.

    My first approach was:

    Qt Code:
    1. //scene_test is just 20KB
    2. QGraphicsPixmapItem* pPixScene = addPixmap(QPixmap(":/Scenes/Scene1/scene_test"));
    3. pPixScene->setPos(-78, -113);
    4. pPixScene = 0;
    To copy to clipboard, switch view to plain text mode 

    Second approach:

    Qt Code:
    1. QBrush brsh;
    2. brsh.setTexture(QPixmap(":/Scenes/Scene1/scene_test"));
    3. this->setBackgroundBrush(brsh);
    To copy to clipboard, switch view to plain text mode 

    This is even worse, and if I try "m_pView->setCacheMode(QGraphicsView::CacheBackground);" it's definitly impossible.

    I think I've tried all the optimization possibilities... for instance:

    Qt Code:
    1. m_pView->setContextMenuPolicy(Qt::NoContextMenu);
    2. m_pView->setDragMode(QGraphicsView::NoDrag);
    3. m_pView->setOptimizationFlags(QGraphicsView::DontSavePainterState |
    4. QGraphicsView::DontAdjustForAntialiasing);
    5. m_pView->setViewportUpdateMode(QGraphicsView::NoViewportUpdate/*QGraphicsView::SmartViewportUpdate*/);
    6. //The updates are called manually. As showed in the next piece of code
    7.  
    8. m_pView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    9. m_pView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    To copy to clipboard, switch view to plain text mode 

    I'm controlling the framerate with the next code. But it only fails when I add the background.

    Qt Code:
    1. void CGameEngine::renderTimer()
    2. {
    3. m_timer.start();
    4.  
    5. renderElements();
    6.  
    7. //Control the updates manually
    8. m_pCurrentScene->update();
    9.  
    10. qDebug() << m_timer.elapsed();
    11. int iElapsed = 40-m_timer.elapsed(); //20 == 50 fps
    12. if(iElapsed > 0){
    13. QTimer::singleShot(iElapsed, this, SLOT(renderTimer()));
    14. }
    15. else
    16. {
    17. qDebug() << "Performance ERROR: " << iElapsed << " ms!!!!!!!";
    18. renderTimer();
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    what I don't understand is why this happens, it takes 2 or 3msec to execute all the code of each frame. Why can it be appearing so slow when I add the background?
    Any idea? thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QGraphicsView performance

    Profile your application and see where the slowdown occurs. Right now you are just guessing, especially that calling update() doesn't redraw your scene but only schedules a redraw. Your redrawing routine (renderTimer()) is probably killing performance of your application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    jano_alex_es (7th February 2011)

  4. #3
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with QGraphicsView performance

    With only three graphics items (one of them, the big pixmap) it's slowing down. With almost two hundred and no big pixmap it goes fine, that's why I think the big pixmap is the problem.

    I knew the problem of the "update" call. But the other option I thought is to call "drawItems" instead. Can I do it in a different manner?

  5. #4
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with QGraphicsView performance

    Well, I can only say "thanks". (actually I want to say it three hundred times :P) Now is going better and I'll try to improve it even more with some "tip and tricks" shown in this video.

    QGraphicsView in depth

    But, on the other hand, I would like to control when the paint is produced (that's why that update()). I can still do it by setting variables in the paint method... but I would like to ask if it's a good idea or I should let Qt go.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QGraphicsView performance

    Your renderTime() method is all wrong. I can't stress it more. I don't know how you measure "slower" and "faster" but if it is based on what your renderTime() method outputs then it is completely useless as it measures the time needed to place an event into the event queue which has nothing to do with redrawing your view. If you want to measure how long it takes for your application to draw your scene then probably the easiest way to do it is this:
    Qt Code:
    1. QElapsedTimer timer;
    2. QPixmap px(800,600); // or similar
    3. timer.start();
    4. scene->render(&px); // or similar
    5. qDebug() << timer.elapsed();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with QGraphicsView performance

    I think you are not understanding why do I need that function. The general idea was to render and paint all the items on the screen each 40 msecs. Now I know this is not true with my code because update() does not paint, just says "calculate and paint whenever you can". I thought the program would stop its executing until the drawing was finished.

    Fortunatly, something failed and I can fix the error.

    On the oher hand, I still need to know how many miliseconds does the render take. That's why that function is still important.

    It seems to work fine removing the update, what I'm planning now is to set a semaphore in the paint method and only allow the painting one time each 40 miliseconds. But I think I should let all the painting stuff to Qt.

Similar Threads

  1. QGraphicsView performance issue
    By dpatel in forum Qt Programming
    Replies: 0
    Last Post: 16th July 2010, 07:50
  2. QgraphicsView performance
    By nileshsince1980 in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2010, 11:54
  3. again QGraphicsView performance
    By medved6 in forum Qt Programming
    Replies: 11
    Last Post: 21st December 2009, 21:11
  4. QGraphicsView performance in 4.6
    By Lodorot in forum Qt Programming
    Replies: 2
    Last Post: 20th September 2009, 23:09
  5. QGraphicsView performance
    By Halabund in forum Newbie
    Replies: 3
    Last Post: 17th April 2009, 10:12

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.