Results 1 to 3 of 3

Thread: QGraphicsItem.setPos takes a lot of time

  1. #1
    Join Date
    Dec 2009
    Location
    Russia, Saint-Petersburg
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem.setPos takes a lot of time

    Hi
    I read in documentation to the Graphics View Framework that visualization of even millions of items performs real-time. To check it I put a little experiment. I made just 50000 items of QGraphicsEllipseItem(qrand() % 500, qrand() % 500, 4, 4) and tried to move it chaotically every half of a second. QGLWidget was selected as viewport. Code of the move() slot to the timer's timeout() signal looked like this:

    Qt Code:
    1. void Visview::move()
    2. {
    3. for (int i = 0; i < 50000; i++)
    4. {
    5. elems[i]->setPos(elems[i]->x() + (qrand() % 20) - 10,
    6. elems[i]->y() + (qrand() % 20) - 10);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    For timer timeout=500 ms it worked extremely slow: about 20 secs to one step of movement. I achieved a big performance improvement by adding

    Qt Code:
    1. scene.update(-500, -500, 1000, 1000);
    To copy to clipboard, switch view to plain text mode 

    to the end of the function. But speed of working is still unsatisfactory to me. About 1.5-2 secs to step, and about 8 secs to first step of movement. I found that the loop with new positions setting

    Qt Code:
    1. for (int i = 0; i < 50000; i++)
    2. {
    3. elems[i]->setPos(elems[i]->x() + (qrand() % 20) - 10,
    4. elems[i]->y() + (qrand() % 20) - 10);
    5. }
    To copy to clipboard, switch view to plain text mode 

    takes about 600 ms to step. Can I make this loop faster somehow?

    PS. I learned source code of the setPos and found it call update function of the scene. But if internal flag updateAll is set to true update() does nothing. To set updateAll to true it's enough to call scene.update(QRect()). I tried to do so before loop, but it took no effect.

    My Qt version is 4.6.0.

    Thank you.
    Last edited by ricofe25; 8th December 2009 at 18:07.

  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: QGraphicsItem.setPos takes a lot of time

    There are many things you can do. For starters set the viewport update mode to FullViewportUpdate. If you're using OpenGL this should already be the case but then passing scene.update() with some parameters shouldn't make any difference. In general when moving all the items it is wise to disable indexing, enabling cache and doing some other things but it depends on a case-by-case basis.
    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:

    ricofe25 (8th December 2009)

  4. #3
    Join Date
    Dec 2009
    Location
    Russia, Saint-Petersburg
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem.setPos takes a lot of time

    Thank you. The FullViewportUpdate mode and indexing disabling have given valuable performance improvements.

Similar Threads

  1. QTextEdit loading takes long time
    By sreedhar in forum Qt Programming
    Replies: 12
    Last Post: 21st March 2011, 11:29
  2. Replies: 4
    Last Post: 3rd August 2009, 21:25
  3. Replies: 1
    Last Post: 1st February 2008, 19:55
  4. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 17:39
  5. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 16:48

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.