Results 1 to 4 of 4

Thread: QGraphicsView/QGraphicsScene/QGraphicsItem slow pos updating

  1. #1
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsView/QGraphicsScene/QGraphicsItem slow pos updating

    I have a lot of items in a QGraphicsScene (about 10000), and I need to update the position of every one of them, but when I do this it can take over 1 minute, so how can I (if possible) make it faster?

    Here's the position update code:

    Qt Code:
    1. void Dialog::updateAllPositions(){
    2. int x,y,z,x2,y2;
    3. for(int aa=0;aa<size.x()*size.y();aa++){
    4. x=aa%size.x();
    5. y=aa/size.x();
    6. z=tiles[y][x];
    7. if(z==0) continue;
    8. x2=(z-1)%size.x();
    9. y2=(z-1)/size.x();
    10. scene->items()[size.x()*size.y()-1-z]->setPos(ssize*x+sgap*x-sgap+(ssize-fontMetric.width(QString::number(z)))/2, ssize*y+sgap*y-sgap+(ssize-fontMetric.height())/2);
    11. scene->items()[2*size.x()*size.y()-2-z]->setPos((x-x2)*ssize+(x-x2)*sgap, (y-y2)*ssize+(y-y2)*sgap);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    ssize and sgap are constants. Even if I disable viewport updating it still takes just as long. Any ideas?

  2. #2
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView/QGraphicsScene/QGraphicsItem slow pos updating

    fixed

    for anyone who has the same problem, heres what I did (even though tbh I dont really understand completely how it works)

    I made a QList<QGraphicsItem*> q and set it equal to scene->items(), then instead of doing scene->items()[stuff]->setPos, do q[stuff]->setPos and it works a lot faster (I dont really understand why it works because scene->items is a const function and I never had to tell the scene that its items had changed, just change them in q)

    Qt Code:
    1. void Dialog::updateAllPositions(){
    2. int x,y,z,x2,y2;
    3. QList<QGraphicsItem*> q=scene->items();
    4. for(int aa=0;aa<size.x()*size.y();aa++){
    5. x=aa%size.x();
    6. y=aa/size.x();
    7. z=tiles[y][x];
    8. if(z==0) continue;
    9. x2=(z-1)%size.x();
    10. y2=(z-1)/size.x();
    11. q[size.x()*size.y()-1-z]->setPos(ssize*x+sgap*x-sgap+(ssize-fontMetric.width(QString::number(z)))/2, ssize*y+sgap*y-sgap+(ssize-fontMetric.height())/2);
    12. q[2*size.x()*size.y()-2-z]->setPos((x-x2)*ssize+(x-x2)*sgap, (y-y2)*ssize+(y-y2)*sgap);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    its faster because it doesnt have to call scene->items thousands of times (each time returning a huge qlist). with 20k items, it only took 0.15 seconds intead of 12 minutes

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView/QGraphicsScene/QGraphicsItem slow pos updating

    Well, QGraphicsView::items is declared with const, but it returns a list of pointer to non-const items. So, you are fine.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Dec 2012
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView/QGraphicsScene/QGraphicsItem slow pos updating

    Quote Originally Posted by spirit View Post
    Well, QGraphicsView::items is declared with const, but it returns a list of pointer to non-const items. So, you are fine.
    ah, that makes sense, thanks

Similar Threads

  1. Replies: 3
    Last Post: 26th October 2011, 11:01
  2. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  3. Updating the width/height of a QGraphicsItem
    By blooglet in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 14:03
  4. Updating the boundingRect of a QGraphicsItem
    By robin2000 in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2009, 08:47
  5. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

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.